Last active
September 30, 2021 14:17
-
-
Save gsong/d02e1bca0a9fc2dc0d6bf79ff123de98 to your computer and use it in GitHub Desktop.
Docker compose file to start PG and PGAdmin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.8" | |
services: | |
pg: | |
image: postgres:alpine | |
container_name: dev-postgres | |
hostname: pg | |
environment: | |
POSTGRES_USER: me | |
POSTGRES_PASSWORD: password | |
POSTGRES_DB: my-db | |
volumes: | |
- ./.data/db:/var/lib/postgresql/data | |
networks: | |
- db | |
healthcheck: | |
test: pg_isready -U postgres || exit 1 | |
stop_grace_period: 1m | |
pgadmin: | |
image: dpage/pgadmin4 | |
container_name: dev-pgadmin | |
hostname: pgadmin | |
environment: | |
PGADMIN_DEFAULT_EMAIL: [email protected] | |
PGADMIN_DEFAULT_PASSWORD: password | |
volumes: | |
- ./.data/pgadmin:/var/lib/pgadmin | |
ports: | |
- 8008:80 | |
depends_on: | |
- postgres | |
networks: | |
- db | |
networks: | |
db: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to Use This
Copy this gist to a file named
docker-compose.yml
in any directory.Start the service containers:
Wait for a few minutes as both services take some time to start up.
Navigate to http://localhost:8008.
Log into PGAdmin using
${PGADMIN_DEFAULT_EMAIL}
and${PGADMIN_DEFAULT_PASSWORD}
(as set indocker-compose.yml
).Click on
Add New Server
underQuick Links
section.Name the server anything you want, e.g.
dev-db
.Click on the
Connection
tab, and fill in the following:pg
${POSTGRES_USER}
orpostgres
if not set${POSTGRES_PASSWORD}
Save
When you're done with everything, destroy the containers:
How to Access PSQL
After you've started the containers:
Using the example above, the command would be:
docker-compose exec -u postgres pg psql my-db me
Notes About docker-compose.yml
pg
POSTGRES_USER
, it defaults topostgres
.POSTGRES_DB
, in this casemy-db
, if you leave out this variable, no database will be created by default.pgadmin
/var/lib/pgadmin
so PGAdmin settings are retained.http://localhost:8008
so we don't try to use a privileged port.networks
We define a network named
db
and attach both services to it. This is what allows us to refer to thepg
service by name rather than by IP address.