Postgres Docker Container

20 Oct 2022

Often, you will want to run a local Postgres instance for development purposes. The best way? Just use docker.

compose.yml
services:
  db:
    image: postgres:15
    restart: always
    user: postgres
    ports:
    - 5432:5432

Keep in mind that if you want to customize the credentials of postgres. You can do that within docker directly:

compose.yml
    image: postgres:15
    environment:
     - POSTGRES_USER=dom
     - POSTGRES_DB=hello
     - POSTGRES_PASSWORD=secret

Or use the automatically sourced .env file:

.env
POSTGRES_USER=dom
POSTGRES_DB=hello
POSTGRES_PASSWORD=secret

Then just start the container:

docker-compose up -d db

Boom! Container is now running and is exposed on port 5432 of your machine. You can alse access psql via:

docker exec -it db psql hello

Connect

Testing