Own PostgreSQL DB using Docker? Yes! Why not?
Why having a docker-compose of a PostgreSQL server could be useful? (You may ask)
First of all, a Docker compose is a tool for defining and running multi-container Docker images. It allows you to configure and run a set of services, such as a database and a Jupyter image, in a single file. This makes it easy to manage and deploy a Data Science environment, as all of its dependencies are bundled together in a single package. I actually use Docker for dividing environments so I have unique environments per project.
In this case, this will help you create your own PostgreSQL server without installing a big database engine like Postgres on your computer, you can turn ON and OFF this server whenever you want, and will help you have clean environments for any Data Science related project (most of all, organize your data).
And, don’t forget the most important part, perfect your SQL skills!!
Steps
- Install Docker Desktop on your computer. I use Docker Desktop because is very User friendly and is easier for starting and stopping containers, but in case you like to use code in bash consoles for docker containers, is up to you!
- For you to create a docker-compose file you can simply open a notepad and copy the following code:
version: '3'
services:
db:
container_name: postgres_db
image: postgres:latest
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: mypassword
POSTGRES_DB: mydb
ports:
- "5432:5432"
volumes:
- ./data:/var/lib/postgresql/data
This file defines a single service named db
that runs the latest version of the PostgreSQL image and names this image as postgres_db
. It also sets several environment variables that configure the PostgreSQL instance, such as the username and password to use, and the name of the database (in this case mydb
) The service also maps the host’s port 5432 to the container’s port 5432 and maps the host’s directory ./data
to the container's /var/lib/postgresql/data
directory, so that the data will persist after the container stops or reboots.
3. Now, save the file in a folder that you will always remember and save it as docker-compose.yml
4. For starting your PostgreSQL database, simply navigate to the directory where your docker-compose.yml
file is located and run the following command:
docker-compose up -d
As soon as you have run this code, you will have something like this on your Docker Desktop:

Where you can Start (play button on the right) or Stop (Stop button in the same place) the image/database whenever you want.
As I said before, this is a very good option for you to have your own PostgreSQL database, and as ChatGPT said after asking “why a docker-compose of a PostgreSQL server is useful?”:
Using Docker Compose to manage your PostgreSQL database provides several benefits. First, it makes it easy to set up and run your database, as all of its dependencies are bundled together in a single package. This also makes it easy to deploy your application, as you can simply copy the
docker-compose.yml
file and other application files to a new machine and run the same command to set up the database. Additionally, Docker Compose allows you to easily scale your database by adding more containers, or change the version of postgres in thedocker-compose.yml
file.
Extra!!
Creating a container with PostgreSQL database and Jupyter Lab image
You can do the same steps mentioned above, but you will need to make a change in your container and add the following:
version: "3"
services:
jupyter:
image: jupyter/scipy-notebook:17aba6048f44
container_name: "Jupyter_notebook"
user: root
ports:
- 8888:8888
environment:
- GRANT_SUDO=yes
volumes:
- ./Notebooks/:/home/jovyan/
postgres:
container_name: postgres_db
image: postgres:12.1-alpine
ports:
- "5432:5432"
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test
volumes:
#- ./dbscripts/:/docker-entrypoint-initdb.d/
- pg_data:/var/lib/postgresql/datavolumes:
pg_data:
The first part of the docker-compose calls the Jupyter docker image and names it “Jupyter_notebook”, the second part is your PostgreSQL database which will be called postgres_db.
You can also use this blog post as a reference for more PostgreSQL docker-compose.