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
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.)

POSTGRES_DB= docker_process_sql --dbname postgres --set db="$POSTGRES_DB" <<-'EOSQL'
CREATE DATABASE :"db" ;
EOSQL
Expand Down