This is the easiest way you can run PostgreSQL in a Docker Container

  • You have to have a Linux installed with Docker CE (27.3.1) installed (https://www.hofsvang.no/?p=165)
  • Make an .env file in directory ~/docker/postgresql

    PG_PASSWORD=TopSecret12345678
    PG_USER=test
    PG_DB=testdb
  • Make a docker-compose.yaml file like this for persistent data in directory ~/docker/postgresql
services:
  db:
    image: postgres:17-alpine
    restart: always
    environment:
      - POSTGRES_PASSWORD=${PG_PASSWORD}
      - POSTGRES_USER=${PG_USER}
      - POSTGRES_DB=${PG_DB}
    ports:
      - 5432:5432
    expose:
      - 5432
    volumes:
      - data:/var/lib/postgresql/data
volumes:
  data: