Python and PostgreSQL Docker Container (Part 1)

The open-source PostgreSQL database management system and Python can do more for you as a developer when you use a Docker container. For example, you can scale and deploy applications wherever you want on the cloud, laptops, and virtual machines. When you dockerize your project in PostgreSQL, you containerize it for workflow streamlining all of the time. Try out the Python PostgreSQL Docker example in this tutorial. It’s Part 1 of a multiple-series which shows you how to build and run a Python and PostgreSQL Docker container.

Prerequisites for Docker

Install the following on your OS to prepare for your Python PostgreSQL Docker project:

NOTE: Docker is available for Linux, macOS, Windows. Your OS should have a minimum RAM of 2GB and an architecture 64-bit framework and a Linux kernel version 3.10 or later. To verify your version number, use the command uname -r.

Install Docker on your host machine

  • You have the option of downloading the interactive Docker installer as an executable for Windows. If you have a macOS X, you can use the installer and get a DMG package. Before you do either, it’s mandatory that you create an account and then log in.

Install Docker on Linux

  • Input this command if you have a distro that is Debian-based, such as Linux Mint or Unbuntu:
sudo apt install docker.io
  • Begin the Docker Engine with this command:
sudo systemctl start docker

Get Docker’s version number

  • From your host machine, go to a command prompt or terminal window and check that you have Docker on it and that it’s running:
docker --version

Run Postgres in Docker

  • Use the command docker run to make a simple Postgre container.

Create a directory for the Docker project

  • From your workspace locally, make a project directory for your new project in Python PostgreSQL Docker. Go into the directory because you’ll use it to keep your data in Postgres.
mkdir postgres-docker && cd postgres-docker
  • While still in the project directory, create a folder for the database and table for PostgreSQL:
mkdir pg-data

Dockerize PostgreSQL

  • Make a Python PostgreSQL Docker container that references some of your database environment variables. Use the command docker run:
docker run -p 5432:5432 -d \
-e POSTGRES_USER="objectrocket" \
-e POSTGRES_PASSWORD="1234" \
-e POSTGRES_DB="some_db" \
-v ${PWD}/pg-data:/var/lib/postgresql/data \
--name pg-container \
postgres # Docker image

NOTE: To run the container where it is detached and won’t receive or display input or output, use the -d. The container will operate in the background.

The flag -v bind mounts to the volume of the project data folder for Postgres. That’s because /var/lib/postgresql/data is the default folder.

An output of Status: Downloaded newer image with the container’s ID right after it means that your host machine doesn’t have the image. After the image is pulled and the container is built, the container should automatically run.

docker run command in a UNIX terminal to pull and start Postgres container from image

TTY interactive terminal shell for the Postgres container

  • To see a list of every container that is running, use this command:
docker ps
  • Connect to the Postgres container. You only have to use the first three figures of its ID. Use the command docker exec:
docker exec -it 1e6 bin/bash

NOTE: The code above shows two flags: the -i which stands for “interactive” mode, and the TTY teletype flag represented by -t. The flags can be passed together or by themselves; however, each flag must be passed to make a Python PostgreSQL Docker Postgres container connection.

docker exec command to enter Postgres container in interactive mode

psql -U objectrocket -d some_db
  • Get a list of the roles in PostgreSQL with the command \du. The user name from the command docker run that you inputted permissions, and attributes are displayed:
root@1e633daa6515:/# psql -U objectrocket -d some_db
psql (12.1 (Debian 12.1-1.pgdg100+1))
Type "help" for help.

some_db=# \du
                                     List of roles
  Role name   |                         Attributes                         | Member of
--------------+------------------------------------------------------------+-----------
 objectrocket | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

some_db=#

NOTE: To obtain a role list, go to the command line prompt in psql and enter the command \du. The user you identified when you ran the command docker run will be shown at the top of the output. That list of roles will display roles, their permissions, and attributes for your Python PostgreSQL Docker examples.

Create Postgres test data

  • Make a PostgreSQL database table for your pg-data directory with the statement CREATE TABLE:
CREATE TABLE some_table (
  id INT PRIMARY KEY NOT NULL,
  int_col INT NOT NULL,
  str_col TEXT NOT NULL,
  bool_col BOOL NOT NULL
);
  • Add some records to your table like this:
INSERT INTO
    some_table(id, str_col, int_col, bool_col)
VALUES
    (1, 'foo bar', 42, TRUE),
    (2, 'ObjectRocket Tutorial', 1234, FALSE);
  • Quit psql with the command \q.

  • Use the command exit to disengage the container connection.

  • Finally, Input the command docker stop and then add the first three figures of the ID for the container.

Screenshot of psql CREATE TABLE statement inside of a Postgres Docker container

Your Python PostgreSQL Docker project has data that is persistent in its folder. This means that the subdirectory /pg-data acts as a backup. The /var/lib/postgresql/data directory is the default is where you can perform a volume bind mount after you create another container.

Conclusion

Utilizing a Python PostgreSQL Docker is a great way to get your work done fast. Deployment, testing, and sharing code is maintained on a high level when developers use an environment that is standardized, yet allows for the freedom to code by imagination. Scale-up, scale down. Containers can help you do it all. Today, you learned how to build a container, make a database connection, show a list of roles, and more. In Part 2, you’ll discover how to set up a Docker Compose file and custom build a Dockerfile image.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.