Skip to content

Commit e61c4a4

Browse files
committed
Add database initialization with custom user and credentials
Supports POSTGRES_USER, POSTGRES_PASSWORD, and POSTGRES_DB environment variables during initdb.
1 parent 9585592 commit e61c4a4

3 files changed

Lines changed: 45 additions & 3 deletions

File tree

cmd/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ Examples:
154154
cmd.Flags().StringVar(&authMethod, "auth-method", lo.CoalesceOrEmpty(os.Getenv("PG_AUTH_METHOD"), string(pkg.AuthScramSHA)), "Authentication method for pg_hba.conf (auto-detected if not specified)")
155155
cmd.Flags().BoolVar(&opts.Enabled, "pg-tune", true, "Run pg_tune to optimize postgresql.conf before starting")
156156
cmd.Flags().Bool("auto-upgrade", true, "Automatically upgrade PostgreSQL if version mismatch detected")
157-
cmd.Flags().Bool("auto-reset-password", true, "Reset postgres superuser password on start")
157+
cmd.Flags().Bool("auto-reset-password", false, "Reset postgres superuser password on start")
158158
cmd.Flags().Bool("auto-init", true, "Automatically initialize database if data directory doesn't exist")
159159
cmd.Flags().Int("upgrade-to", 0, "Target PostgreSQL version for upgrade (default: auto-detect latest)")
160160

docker-compose.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
services:
2+
postgres:
3+
container_name: "pg_test"
4+
build:
5+
context: .
6+
dockerfile: Dockerfile
7+
args:
8+
PG_VERSION: 17
9+
image: flanksource/postgres:17
10+
environment:
11+
POSTGRES_DB: mc
12+
POSTGRES_USER: flanksource
13+
POSTGRES_PASSWORD: testpassword
14+
ports:
15+
- "5435:5432"
16+
volumes:
17+
- pg-data-test:/var/lib/postgresql/data
18+
19+
volumes:
20+
pg-data-test:

docker-entrypoint.sh

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,34 @@ fi
3232

3333
export PGBIN=/usr/lib/postgresql/${PG_VERSION}/bin
3434

35+
# Set defaults for PostgreSQL environment variables
36+
POSTGRES_USER="${POSTGRES_USER:-postgres}"
37+
POSTGRES_DB="${POSTGRES_DB:-$POSTGRES_USER}"
3538

3639
if [ ! -f $PGDATA/PG_VERSION ]; then
3740
echo "Initializing database cluster at $PGDATA ..."
38-
# starting and stopping the DB to initialize the directory for tuning
39-
$PGBIN/initdb -D $PGDATA
41+
echo "Using POSTGRES_USER: $POSTGRES_USER"
42+
43+
# Initialize database with specified user and password
44+
if [ -n "$POSTGRES_PASSWORD" ]; then
45+
echo "Initializing with password authentication"
46+
$PGBIN/initdb -D $PGDATA -U "$POSTGRES_USER" --pwfile=<(printf "%s\n" "$POSTGRES_PASSWORD")
47+
else
48+
echo "WARNING: No password set. Initializing without password authentication."
49+
$PGBIN/initdb -D $PGDATA -U "$POSTGRES_USER"
50+
fi
51+
52+
# Start PostgreSQL temporarily to create database
4053
$PGBIN/pg_ctl start -D $PGDATA --wait
54+
55+
# Create custom database if specified and different from default
56+
if [ "$POSTGRES_DB" != "postgres" ]; then
57+
echo "Creating database: $POSTGRES_DB"
58+
$PGBIN/psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname postgres <<-EOSQL
59+
CREATE DATABASE "$POSTGRES_DB" OWNER "$POSTGRES_USER";
60+
EOSQL
61+
fi
62+
4163
$PGBIN/pg_ctl stop -D $PGDATA --wait
4264
else
4365
echo "Database cluster already initialized at $PGDATA with version $(cat $PGDATA/PG_VERSION)"

0 commit comments

Comments
 (0)