How to run PostgreSQL using Docker

How to run PostgreSQL using Docker

Tags
Web Dev
Docker
Published
February 5, 2023
Author
Abod
Back when I started learning programming, specially web development, when watching tutorials it is required to have a database, some developers like to use SQLite, which is great if you don’t want to bother with more sophisticated databases, like MySQL or PostgreSQL.
PostgreSQL was always really difficult to set it up, there are always issues from compatibility issues, different operating systems, to mistakes I accidently made in the setup process, and it was a really long setup process. But it doesn’t have to be anymore.

What’s Docker?

Since docker was announced, it has changed a lot of things in the development eco-system, from simplifying deployment, to apps that are ready to use by just writing a docker command to run the service locally, without having to worry about any dependencies, missing packages or any of that.
According to Wikipedia,
Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers. The service has both free and premium tiers. The software that hosts the containers is called Docker Engine. It was first started in 2013 and is developed by Docker, Inc.
But the way I like to think of Docker, it is a programmable virtualized operating system. With ready-to-use-system-images.
You can run any operating system you want inside a Docker container, and configure it however deemed necessary using the Dockerfile, from running different bash commands to assigning environment variables.
Its main selling point, is a unified environment, you don’t have to worry about your app running in a different OS, it will always run in the OS you specify, will install the packages you want, with the exact versions needed. So no-more “It worked on my machine”.
 
notion image

Why use PostgreSQL using Docker?

Trying to install and running PostgreSQL directly, will require you to install multiple packages that might have different versions, and all sorts of compatibility issues you might face, but using Docker ensures everything is consistent. And with ready-images you can install a ready to use PostgreSQL, and it will have everything setup and you can get started within seconds.

Installing Docker

Before you can run a PostgreSQL database using Docker, you’ll need to have Docker installing in your machine, the setup is different based on your operating system, hardware, etc..
I highly recommend you follow the official guide of installation based on your operating system. Linux, Windows and Mac

How to start the PostgreSQL database

To confirm you have Docker installed, run the following command on your terminal
$ docker --version
You should get output similar to this:
Docker version 20.10.21, build baeda1f
 
Now that you’ve confirmed you have Docker, let’s run PostgreSQL, type the following command in your terminal
docker run --name postgres-db -e POSTGRES_PASSWORD=password -p 5432:5432 -d postgres
What this command does, it will run a new Docker container,
—name the name of the created container will be postgres-db,
-e will set the environment variable POSTGRES_PASSWORD to “password” which will be the password we use when connecting to the database, I highly recommend you chose a more secure one 😅
-p makes the database port 5432 on the container to be available in the host machine (your machine) under the same port, so you can easily access it from other apps, for example PgAdmin.
-d the image you want to use, since we’re not specifying any versions, we will get the latest version available in Docker Hub.
After running that command, you should get a long randomly generated string printed in your terminal, something like this:
notion image
To ensure the database container is still running, execute the following command
docker ps
This commands lists the currently running containers.
notion image
As you can see, it was created a few seconds ago, and the name we set is visible in the most right, under the NAMES column.

Connecting to the database

Now since our database is running, we can easily connect to it, there are multiple ways to do that, but I prefer using the PgAdmin software, which you can install from here, if you don’t have it already.

Adding database to Pgadmin

After opening PgAdmin, you can add a server by right clicking on the server icon, in the left bar
notion image
Then Register > Server
Give it a name
notion image
Then in the “Connection” tab fill in the connection details, like this. In the password field, type the password you set when running PostgreSQL using docker
notion image
After you click save, it should automatically connect to the database, and it will show in the sidebar like this:
notion image
There are a lot of things you can customize of your PostgreSQL setup, using the environment variables. I would advise you look at the reference here in the How to extend this image section.
 
That’s it, now you’ve got a fully functional PostgreSQL database running, that you can use to develop your apps. In the following article, we will see how we can connect to this database from our Python app.
 
Thank you for reading all the way to here.
You can find me in one of the following websites: TwitterGitHubLinkedIn