Add database initialization with custom user and database#31
Add database initialization with custom user and database#31adityathebe merged 2 commits intomainfrom
Conversation
Supports POSTGRES_USER, POSTGRES_PASSWORD, and POSTGRES_DB environment variables during initdb.
b3d88e1 to
db33aa5
Compare
db33aa5 to
a32706e
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR adds support for PostgreSQL environment variable configuration (POSTGRES_USER, POSTGRES_DB, POSTGRES_PASSWORD) in the Docker entrypoint script and introduces comprehensive integration tests to verify the functionality. The key change modifies the default behavior of the auto-reset-password flag to be more secure by default.
- Adds environment variable support for custom PostgreSQL user, database, and password during initialization
- Changes the default value of
auto-reset-passwordflag fromtruetofalsefor improved security - Implements comprehensive integration tests to validate custom database and user creation
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| docker-entrypoint.sh | Adds logic to set defaults for POSTGRES_USER and POSTGRES_DB, handles custom user/password initialization, and creates custom databases if specified |
| docker-compose.yaml | Adds new Docker Compose configuration for testing with custom environment variables |
| cmd/main.go | Changes default value of auto-reset-password flag from true to false |
| .github/workflows/test.yml | Adds new test-env-variables job with comprehensive tests for custom database, user, and authentication verification |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| $PGBIN/pg_ctl start -D $PGDATA --wait | ||
|
|
||
| # Create custom database if specified and different from default | ||
| if [ "$POSTGRES_DB" != "postgres" ]; then |
There was a problem hiding this comment.
The condition checks if POSTGRES_DB is different from 'postgres', but according to line 37, POSTGRES_DB defaults to $POSTGRES_USER. If a custom user is specified without a custom database, this will incorrectly skip database creation when POSTGRES_USER equals 'postgres'. The condition should check if POSTGRES_DB is different from the default database for the user, which would be created automatically by initdb.
| if [ "$POSTGRES_DB" != "postgres" ]; then | |
| if [ "$POSTGRES_DB" != "$POSTGRES_USER" ]; then |
| 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 |
There was a problem hiding this comment.
When initdb is run with -U flag, it creates a database with the same name as the specified user. This means if POSTGRES_USER=flanksource and POSTGRES_DB=flanksource, the database already exists and the CREATE DATABASE statement will fail. The condition should check if the database name differs from both 'postgres' AND the username to avoid attempting to create a database that already exists.
| 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"; |
There was a problem hiding this comment.
The SQL statement uses double quotes around the database name variable, which will fail if POSTGRES_DB contains double quotes. Consider using identifier quoting or escaping to prevent SQL injection and syntax errors. PostgreSQL's CREATE DATABASE should quote the identifier properly, such as using CREATE DATABASE ${POSTGRES_DB@Q} or validating the database name format.
Summary
Supports POSTGRES_USER, POSTGRES_PASSWORD, and POSTGRES_DB environment variables during database initialization.