Skip to content

Commit d07170d

Browse files
committed
fix user creation
1 parent 4057dda commit d07170d

2 files changed

Lines changed: 14 additions & 4 deletions

File tree

Containerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ FROM ghcr.io/daemonless/base:${BASE_VERSION}
44
ARG PG_VERSION
55

66
ARG FREEBSD_ARCH=amd64
7-
ARG PACKAGES="postgresql${PG_VERSION}-server postgresql${PG_VERSION}-client"
7+
ARG PACKAGES="postgresql${PG_VERSION}-server postgresql${PG_VERSION}-client postgresql${PG_VERSION}-contrib"
88
ARG UPSTREAM_URL="https://www.postgresql.org/docs/release/"
99
ARG HEALTHCHECK_ENDPOINT="pg_isready -q"
1010

root/etc/services.d/postgresql/run

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,23 @@ if [ ! -f "$PGDATA/PG_VERSION" ]; then
2727
# Start temporarily to create user/database and run init scripts
2828
/usr/local/bin/s6-setuidgid bsd /usr/local/bin/pg_ctl -D "$PGDATA" -w start
2929

30-
# Create user and database if specified
30+
# Create user if not default 'bsd' user
31+
PGUSER="${POSTGRES_USER:-postgres}"
32+
if [ "$PGUSER" != "bsd" ]; then
33+
echo "Creating user '$PGUSER'..."
34+
/usr/local/bin/s6-setuidgid bsd /usr/local/bin/psql -d postgres -c "CREATE ROLE $PGUSER WITH LOGIN SUPERUSER" 2>/dev/null || true
35+
fi
36+
37+
# Set password if specified
3138
if [ -n "$POSTGRES_PASSWORD" ]; then
32-
/usr/local/bin/s6-setuidgid bsd /usr/local/bin/psql -c "ALTER USER $POSTGRES_USER PASSWORD '$POSTGRES_PASSWORD'" 2>/dev/null
39+
echo "Setting password for '$PGUSER'..."
40+
/usr/local/bin/s6-setuidgid bsd /usr/local/bin/psql -d postgres -c "ALTER ROLE $PGUSER PASSWORD '$POSTGRES_PASSWORD'" 2>/dev/null
3341
fi
3442

43+
# Create database if specified and not 'postgres'
3544
if [ -n "$POSTGRES_DB" ] && [ "$POSTGRES_DB" != "postgres" ]; then
36-
/usr/local/bin/s6-setuidgid bsd /usr/local/bin/createdb "$POSTGRES_DB" 2>/dev/null
45+
echo "Creating database '$POSTGRES_DB'..."
46+
/usr/local/bin/s6-setuidgid bsd /usr/local/bin/createdb -O "$PGUSER" "$POSTGRES_DB" 2>/dev/null || true
3747
fi
3848

3949
# Run init scripts from /docker-entrypoint-initdb.d/

0 commit comments

Comments
 (0)