Docker containers provide a convenient way to manage and deploy applications. PostgreSQL, a powerful open-source relational database system, can be run in a Docker container for easy development, testing, and deployment. This guide will show you how to set up and run a PostgreSQL Docker container.

In this guide, we’ll walk through the steps to create a PostgreSQL Docker container.

Prerequisites

Before we begin, ensure you have the following:

  • Docker installed on your machine.
  • Basic knowledge of Docker commands and PostgreSQL.

Step 1 – Pulling the PostgreSQL Docker Image

First, let’s pull the official PostgreSQL Docker image from Docker Hub. Open your terminal and run the following command:

docker pull postgres

This command will download the latest PostgreSQL image. If you need a specific version, you can specify it like this:

docker pull postgres:

For example, to pull PostgreSQL 13, you would run:

docker pull postgres:13

Step 2 – Running a PostgreSQL Container

Now that we have the PostgreSQL image, let’s run a container. We’ll run the container with some basic configurations such as setting the PostgreSQL password and mapping a port.

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

Explanation:

  • –name my_postgres: Names the container my_postgres.
  • -e POSTGRES_PASSWORD=mysecretpassword: Sets the PostgreSQL password to mysecretpassword.
  • -d: Runs the container in detached mode.
  • -p 5432:5432: Maps port 5432 of the host to port 5432 of the container.
  • postgres: Uses the postgres image.

To verify that the container is running, use:

docker ps

Output:

CONTAINER ID   IMAGE       COMMAND                  CREATED         STATUS         PORTS                                       NAMES
f690208d6e8a   postgres    "docker-entrypoint.s…"   6 seconds ago   Up 6 seconds   0.0.0.0:5432->5432/tcp, :::5432->5432/tcp   my_postgres

Step 3 – Connecting to the PostgreSQL Container

You can connect to the PostgreSQL database using the psql command-line tool or any PostgreSQL client like pgAdmin. For this example, we’ll use psql.

First, enter the running container:

docker exec -it my_postgres bash

Once inside the container, connect to PostgreSQL using:

psql -U postgres

Once connected, you will see the following output:

psql (16.3 (Debian 16.3-1.pgdg120+1))
Type "help" for help.

postgres=# 

To exit from the Postgres shell, run:

exit

To exit from the container, run:

exit

Step 4 – Managing the PostgreSQL Container

To stop the PostgreSQL container, run:

docker stop my_postgres

To start the PostgreSQL container again, run:

docker start my_postgres

To remove the container, first stop it (if it’s running), and then remove it:

docker rm my_postgres

Step 4 – Data Persistence

By default, data inside a Docker container is ephemeral, meaning it will be lost when the container is removed. To persist data, you can use Docker volumes.

Create a volume:

docker volume create pgdata

Run the PostgreSQL container with the volume:

docker run --name mynew_postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 -v pgdata:/var/lib/postgresql/data postgres

The -v pgdata:/var/lib/postgresql/data option mounts the volume to the PostgreSQL data directory.

Conclusion

In this guide, we’ve covered how to create and manage a PostgreSQL Docker container. We’ve walked through pulling the PostgreSQL image, running a container, connecting to it, and ensuring data persistence. Using Docker for PostgreSQL provides a flexible and efficient way to manage your databases, especially in development and testing environments. Let’s create a PostgreSQL docker container on dedicated server hosting from Atlantic.Net!