-
Notifications
You must be signed in to change notification settings - Fork 0
Add database initialization with custom user and database #31
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| services: | ||
| postgres: | ||
| container_name: "pg_test" | ||
| build: | ||
| context: . | ||
| dockerfile: Dockerfile | ||
| args: | ||
| PG_VERSION: 17 | ||
| image: flanksource/postgres:17 | ||
| environment: | ||
| POSTGRES_DB: mc | ||
| POSTGRES_USER: flanksource | ||
| POSTGRES_PASSWORD: testpassword | ||
| ports: | ||
| - "5435:5432" | ||
| volumes: | ||
| - pg-data-test:/var/lib/postgresql/data | ||
|
|
||
| volumes: | ||
| pg-data-test: |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,12 +32,34 @@ fi | |
|
|
||
| export PGBIN=/usr/lib/postgresql/${PG_VERSION}/bin | ||
|
|
||
| # Set defaults for PostgreSQL environment variables | ||
| POSTGRES_USER="${POSTGRES_USER:-postgres}" | ||
| POSTGRES_DB="${POSTGRES_DB:-$POSTGRES_USER}" | ||
|
|
||
| if [ ! -f $PGDATA/PG_VERSION ]; then | ||
| echo "Initializing database cluster at $PGDATA ..." | ||
| # starting and stopping the DB to initialize the directory for tuning | ||
| $PGBIN/initdb -D $PGDATA | ||
| echo "Using POSTGRES_USER: $POSTGRES_USER" | ||
|
|
||
| # Initialize database with specified user and password | ||
| if [ -n "$POSTGRES_PASSWORD" ]; then | ||
| echo "Initializing with password authentication" | ||
| $PGBIN/initdb -D $PGDATA -U "$POSTGRES_USER" --pwfile=<(printf "%s\n" "$POSTGRES_PASSWORD") | ||
| else | ||
| echo "WARNING: No password set. Initializing without password authentication." | ||
| $PGBIN/initdb -D $PGDATA -U "$POSTGRES_USER" | ||
| fi | ||
|
|
||
| # Start PostgreSQL temporarily to create database | ||
| $PGBIN/pg_ctl start -D $PGDATA --wait | ||
|
|
||
| # Create custom database if specified and different from default | ||
| if [ "$POSTGRES_DB" != "postgres" ]; then | ||
| echo "Creating database: $POSTGRES_DB" | ||
| $PGBIN/psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname postgres <<-EOSQL | ||
| CREATE DATABASE "$POSTGRES_DB" OWNER "$POSTGRES_USER"; | ||
|
||
| EOSQL | ||
| fi | ||
|
Comment on lines
+56
to
+61
|
||
|
|
||
| $PGBIN/pg_ctl stop -D $PGDATA --wait | ||
| else | ||
| echo "Database cluster already initialized at $PGDATA with version $(cat $PGDATA/PG_VERSION)" | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The condition checks if
POSTGRES_DBis different from 'postgres', but according to line 37,POSTGRES_DBdefaults to$POSTGRES_USER. If a custom user is specified without a custom database, this will incorrectly skip database creation whenPOSTGRES_USERequals 'postgres'. The condition should check ifPOSTGRES_DBis different from the default database for the user, which would be created automatically byinitdb.