Skip to main content
Docker

How to run PostgreSQL using Docker?

Prerna Sharma

How to run Postgres in Docker?

To start with first download the latest Docker Desktop release and install it. Docker Desktop includes the Docker CLI, Docker Compose, and required development tools. Meanwhile, the Docker Desktop will help you manage images and containers. 

Afterward, it’s time to Dockerize Postgres.

Pull postgres Docker Image

We need to pull the latest docker image of postgres from Docker Hub.
Fire the below command in your terminal.

docker pull postgres

Alternatively, you can pin your preferred version with a specific tag like docker pull postgres:14.5.

Docker will download your postgres image locally onto your machine. Here’s how the output looks like.

Using default tag: latest
latest: Pulling from library/postgres
2c6d21737d83: Pull complete 
cd877d542a82: Pull complete 
ef4dec12b244: Pull complete 
b4793e74422c: Pull complete 
12f6bc37ee29: Pull complete 
fd9c37dc95ed: Pull complete 
413b8cf9d665: Pull complete 
1c95e694bd99: Pull complete 
6b899778c1e5: Pull complete 
2627f21dbdce: Pull complete 
de237cbda09b: Pull complete 
f6db1b4908f2: Pull complete 
f5f0f93ae856: Pull complete 
259820b698b9: Pull complete 
Digest: sha256:ff37e66b0a03594086c3734d73e750f13480ca9bf64b53fafea18be4d5afb9ad
Status: Downloaded newer image for postgres:latest
docker.io/library/postgres:latest

Next, if you want to see all the docker images present in your machine, you can run the below command and look for postgres or you can also confirm this within Docker Desktop.

docker image ls

You will be able to see one of the docker images of postgres on your machine.

REPOSITORY             TAG                    IMAGE ID       CREATED         SIZE
postgres               latest                 b9d4095a181a   7 days ago      455MB
Docker Desktop

Once we have the docker image postgres on our machine, it's time to run a container with this image. To do so, fire the below command in your terminal.

Start a Postgres instance

docker run -d --name my-postgres -p 5445:5432 -e POSTGRES_PASSWORD=mysecretpassword postgres

The above command creates a container named my-postgres in detached mode and assigns important environment variables before running everything in the background. You will see something like this:

877ad2dea06b68b44cd4f8b499c447ae4ddfc91ba85aa54bc4670918576b69ee

Let's see the above command in detail.

  1. docker run: This command is used to run a Docker container.
  2. -d: It stands for "detached" mode, which means the container runs in the background.
  3. --name my-postgres: It assigns the name "my-postgres" to the container, making it easier to refer to it instead of using the container ID.
  4. -p 5445:5432: It maps port 5445 on the host to port 5432 on the container. This is done to allow connections to the PostgreSQL database running inside the container from the host machine through port 5445. You can give any port.
  5. -e POSTGRES_PASSWORD=mysecretpassword: It sets the environment variable POSTGRES_PASSWORD with the value "mysecretpassword". This is the password that will be set for the default PostgreSQL user, 'postgres'.
  6. postgres: It specifies the Docker image to be used for the container, in this case, it's the official PostgreSQL image.

Now, it's time to exec into the container my-postgres running inside docker.

docker exec -it my-postgres bash

After running the above command you will be now inside my-postgres container running in your docker. The command is used to start an interactive shell session inside a running Docker container named "my-postgres." It provides a way to interact with the container's environment and run commands as if you were inside the container's terminal. The bash at the end specifies that the shell being used is Bash.

root@877ad2dea06b:/#

At this moment, we are now inside my-postgres the container running.

root@11fe4a72d319:/# psql -h localhost -U postgres

The above command is used to connect to a local PostgreSQL database using the username postgres inside the container. You will see something like this in the output.

postgres=#

Create a database employee by running the below command in both terminals.

postgres=# CREATE DATABASE employee;

If you find the below output, then the database is created successfully.

CREATE DATABASE

Now, you have successfully created a database in my-postgres container running inside docker. Now, it's time to create tables and whatever stuff you want to do within this database. For ease, I would recommend you use DBeaver to connect to this database. You can connect from pgAdmin Once you have downloaded it, follow the below steps:

Step 1: Click on the top-left corner New Database Connection. This will open a window to select the database you want to connect to. Select PostgreSQL and click on Next.

Step 2: Now fill in all the details and click on finish as shown below. You can click on Test Connection first to test the connection and then you can click on Finish.

And you are now connected to this database. Now you can create tables or import the data.

Connect with us on Discord in case you are stuck or have any questions.