Skip to content

Commit

Permalink
Docker Compose creates postgres containers safely initialise the DB
Browse files Browse the repository at this point in the history
POSTGRES_DB
---
This optional environment variable can be used to define a different name for the default database that is created when the image is first started. If it is not specified, then the value of POSTGRES_USER will be used.[1]

Before, when we were providing a POSTGRES_USER we used the value of "postgres".  This instructed postgres to create a database that did not match the name app-test or rails-template-development (depending on your environment). For some reason, this worked for a time but we noticed that throwing away all local database state within volumes, we got a database not found error on boot for any environment.

This change explicitly instructs Postgres as to the name of the database we expect it to create. It should match the value we use at the end of DATABASE_URL.

[1] https://hub.docker.com/_/postgres/
  • Loading branch information
tahb committed Mar 29, 2022
1 parent ffa6f4f commit 9577edb
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 0 deletions.
1 change: 1 addition & 0 deletions docker-compose.ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ services:
test-db:
image: postgres
environment:
POSTGRES_DB: app-test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_HOST_AUTH_METHOD: trust
Expand Down
1 change: 1 addition & 0 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ services:
volumes:
- db-data:/var/lib/postgresql/data
environment:
POSTGRES_DB: app-test
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
POSTGRES_HOST_AUTH_METHOD: trust
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ services:
volumes:
- db-data:/var/lib/postgresql/data
environment:
# TODO: Update rails-template with application name
POSTGRES_DB: rails-template-development
POSTGRES_PASSWORD: password
POSTGRES_HOST_AUTH_METHOD: trust
networks:
Expand Down

0 comments on commit 9577edb

Please sign in to comment.