Run PostgreSQL Using Docker with Docker Compose

Running PostgreSQL in a Docker container makes it incredibly easy to spin up a database without installing anything on our local machine. In this article, we’ll walk through how to set up PostgreSQL using Docker Compose in a simple and friendly way.

Tables of contents:

Technologies used:

  • Docker 28
  • Official Docker postgres image (latest tag)

1. Create a docker-compose.yml file

Create a file named docker-compose.yml and add the following content:

docker-compose.yml

services:
  postgres:
    image: postgres:latest
    container_name: my_postgres
    restart: always
    environment:
      POSTGRES_USER: mkyong
      POSTGRES_PASSWORD: password
      POSTGRES_DB: testdb
    ports:
      - "5432:5432"
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:
    driver: local

2. Start PostgreSQL Container

Run this command in the same directory as our docker-compose.yml file:

Terminal

docker-compose up -d

This will:

  • Pull the latest PostgreSQL image (if not already present),
  • Create a container named my_postgres
  • Map port 5432 from the container to our local machine.

3. Connect to PostgreSQL

We can connect to the running database in two ways:

Option 1: Using Docker CLI with psql command

Run the following command to access the PostgreSQL CLI:

Terminal

docker exec -it my_postgres psql -U mkyong -d testdb

This will bring up the PostgreSQL prompt:

Terminal

docker exec -it my_postgres psql -U mkyong -d testdb
psql (17.4 (Debian 17.4-1.pgdg120+2))
Type "help" for help.

testdb=# \l

Access the PostgreSQL CLI and show all tables:

Terminal

docker exec -it my_postgres psql -U mkyong -d testdb
psql (17.4 (Debian 17.4-1.pgdg120+2))
Type "help" for help.

testdb-# \dt

             List of relations
 Schema |       Name       | Type  | Owner  
--------+------------------+-------+--------
 public | question         | table | mkyong
 public | question_options | table | mkyong
 public | quiz             | table | mkyong
(3 rows)

Option 2: Use a GUI Client

Tools like pgAdmin, DBeaver, or DataGrip make it easy to manage our databases visually.

Use the following connection details:

  • Host: localhost
  • Port: 5432
  • Database: testdb
  • User: mkyong
  • Password: password

4. Stop and Remove Containers

To stop the PostgreSQL container:

Terminal

docker-compose down

If we want to remove the volumes (i.e., delete all data), add the -v flag:

Terminal

docker-compose down -v

P.S This will permanently delete the database and all its contents. Use with caution.

5. Restarting Without Losing Data

Docker volumes allow us to persist PostgreSQL data even after restarting or recreating containers. The volumes section in our docker-compose.yml ensures this.

docker-compose.yml

services:
  postgres:
    image: postgres:latest
    container_name: my_postgres
    restart: always
    environment:
      POSTGRES_USER: mkyong
      POSTGRES_PASSWORD: password
      POSTGRES_DB: testdb
    ports:
      - "5432:5432"
    # Take the volume named `postgres_data`, and map it to the internal PostgreSQL data directory."
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:
    driver: local

To test this, stop the container:

Terminal

docker-compose down

Then start it again:

Terminal

docker-compose up -d

The data will still be available because it’s stored in the postgres_data volume.

6. Use System Environment Variables

For better security and flexibility, we can store sensitive data in system environment variables instead of hardcoding them.

docker-compose.yml

environment:
  POSTGRES_USER: ${POSTGRES_USER}
  POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
  POSTGRES_DB: ${POSTGRES_DB}

Then, in our terminal or .env file, define:

.env

POSTGRES_USER=mkyong
POSTGRES_PASSWORD=password
POSTGRES_DB=testdb

This approach is very helpful in CI/CD pipelines or when working on teams.

7. References

Image

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments