Skip to content

Do not try to create database 'template1' #794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

reda-alaoui
Copy link

@reda-alaoui reda-alaoui commented Dec 3, 2020

When passing POSTGRES_DB=template1, docker-entrypoint.sh tries to create database template1. It fails since template1 is always present by default.
docker-entrypoint.sh already skips postgres database creation. This changes makes docker-entrypoint.sh skip template1 database creation.

@reda-alaoui reda-alaoui force-pushed the do-not-create-template1 branch from e534056 to 8d9c329 Compare December 3, 2020 11:22
@@ -188,7 +188,7 @@ docker_process_sql() {
# create initial database
# uses environment variables for input: POSTGRES_DB
docker_setup_db() {
if [ "$POSTGRES_DB" != 'postgres' ]; then
if [ "$POSTGRES_DB" != 'postgres' ] || [ "$POSTGRES_DB" != 'template1' ]; then
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The entrypoint already notes that initdb will create the three databases:

# `initdb` automatically creates the "postgres", "template0", and "template1" dbnames

And the intent of this "$POSTGRES_DB" != 'postgres' is to not create an already created database (since postgresql doesn't have an IF NOT EXISTS on create database), it might make more sense to key off the database name already existing? (just in case initdb changes in the future)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬 great point (and https://stackoverflow.com/a/18389184/433558 is depressing 😞)

So maybe something like this?

Suggested change
if [ "$POSTGRES_DB" != 'postgres' ] || [ "$POSTGRES_DB" != 'template1' ]; then
local exists
if exists="$(POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" --tuples-only --command 'SELECT 1 FROM pg_database WHERE datname = :"db"')" && [ -z "$exists" ]; then

(Sorry it's not super obvious, but this will also need to be copied to every directory for the GitHub Actions to test it properly.)

@reda-alaoui reda-alaoui deleted the do-not-create-template1 branch December 28, 2020 22:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants