← Back
config docker-compose pgadmin postgres

My new favourite way to run postgres

As you already know, I don't have postgres installed (nor do I intend to)

My new project requires a postgres instance, so this is my new favourite configuration:

docker-compose.yml:

version: "3.4"

services:
postgres:
image: postgres
restart: unless-stopped
volumes:
- postgres:/var/lib/postgresql/data
ports:
- 5432:5432

pgadmin:
image: dpage/pgadmin4
restart: always
depends_on:
- postgres
ports:
- 5050:80
volumes:
- pgadmin:/var/lib/pgadmin
environment:
PGADMIN_DEFAULT_EMAIL: pgadmin@example.com
PGADMIN_DEFAULT_PASSWORD: admin

volumes:
postgres:
pgadmin:
node_modules:

Now i can docker-compose up postgres to start the application

pgadmin #

And docker-compose up pgadmin to start pgadmin at http://localhost:5050 👇 pgadmin awesome dashboard

Instructions:

Extra #

If you like npm scripts:

{
"scripts": {
"start": "docker-compose -p prj-name up -d postgres",
"pgadmin": "docker-compose -p prj-name up -d pgadmin",
"stop": "docker-compose -p prj-name down",
"bash": "docker-compose -p prj-name run --rm --service-ports bash",
"purge": "docker-compose -p prj-name down --volumes"
}
}

Extra: create database automatically #

Docker image creates an empty database named (surpise!) the same as the user and password: postgres. You can change the name using enviroment variables. Take care: env variables must be written as an array:

docker-compose.yml:

version: "3.4"

services:
postgres:
image: postgres
restart: unless-stopped
volumes:
- postgres:/var/lib/postgresql/data
environment:
- POSTGRES_USER: development
- POSTGRES_PASSWORD: secret
- POSTGRES_DB: my-project-dev
ports:
- 5432:5432

Sources #