How to start a local PostgreSQL database with Docker

Bjorn Krols

Bjorn Krols

Published on
27 July 2021

docker-compose.yml

version: "3"
services:
  postgres:
    image: "postgres"
    container_name: "postgres"
    environment:
      - POSTGRES_USER=john
      - POSTGRES_PASSWORD=shhhht
      - POSTGRES_DB=project
    ports:
      - 5432:5432

To start the service:

docker compose up -d

To access PostgreSQL's interactive shell:

docker exec -it postgres psql -U john -W shhhht project
  • docker exec -it - run a command to a running container
  • postgres - the container name
  • psql - the command
  • -U john - the user
  • -W shhhht - the password
  • project - the database you want to connect to

To exit the shell:

exit

To stop the service:

docker compose down

Subscribe to our newsletter

The latest news, articles, and resources, sent to your inbox weekly.

More like this