Skip to content

Commit

Permalink
fix(initdb): add workaround for timescaledb initdb
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazmi35 committed Oct 9, 2024
1 parent bd8dae2 commit 0f3cb07
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
1 change: 1 addition & 0 deletions 16/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ COPY --from=timescaledb /tmp/share/* /opt/bitnami/postgresql/share/extension/

# TimescaleDB Docker initialization scripts
COPY --from=timescaledb /docker-entrypoint-initdb.d/*.sh /docker-entrypoint-initdb.d/
COPY ./initdb/* /docker-entrypoint-initdb.d/

# Entrypoint
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
Expand Down
1 change: 1 addition & 0 deletions 17/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ COPY --from=timescaledb /tmp/share/* /opt/bitnami/postgresql/share/extension/

# TimescaleDB Docker initialization scripts
COPY --from=timescaledb /docker-entrypoint-initdb.d/*.sh /docker-entrypoint-initdb.d/
COPY ./initdb/* /docker-entrypoint-initdb.d/

# Entrypoint
COPY docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh
Expand Down
30 changes: 30 additions & 0 deletions initdb/000_alter_owner_temp.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

. /opt/bitnami/scripts/liblog.sh

# This is made so TimescaleDB initdb CREATE DATABASE will work.

if [ -z "${POSTGRESQL_POSTGRES_PASSWORD:-}" ]; then
POSTGRESQL_POSTGRES_PASSWORD=${POSTGRES_POSTGRES_PASSWORD:-}
fi

export PGPASSWORD="$POSTGRESQL_POSTGRES_PASSWORD"

if [ $POSTGRES_USER != "postgres" ]; then
# IF pgpassword is not set, fail
if [ -z "${PGPASSWORD:-}" ]; then
error "If you set POSTGRES_USER to a value other than 'postgres', you need to set POSTGRESQL_POSTGRES_PASSWORD or POSTGRES_POSTGRES_PASSWORD environment variable"
error "This is because bitnami image does not make the POSTGRES_USER a superuser"
exit 1
fi

# Find "timescaledb.telemetry_level" in the postgresql.conf file
if grep -q "timescaledb.telemetry_level" /bitnami/postgresql/conf/postgresql.conf; then
# Change the owner of the initial databases to the POSTGRES_USER
psql -U postgres postgres -f "ALTER DATABASE postgres OWNER TO $POSTGRES_USER;"
psql -U postgres template1 -f "ALTER DATABASE template1 OWNER TO $POSTGRES_USER;"

# Change the owner of the POSTGRES_DB to the POSTGRES_USER
psql -U postgres $POSTGRES_DB -f "ALTER DATABASE $POSTGRES_DB OWNER TO $POSTGRES_USER;"
fi
fi
30 changes: 30 additions & 0 deletions initdb/999_alter_owner_temp_revert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

. /opt/bitnami/scripts/liblog.sh

# This is reverting the changes made in 000_alter_owner_temp.sh

if [ -z "${POSTGRESQL_POSTGRES_PASSWORD:-}" ]; then
POSTGRESQL_POSTGRES_PASSWORD=${POSTGRES_POSTGRES_PASSWORD:-}
fi

export PGPASSWORD="$POSTGRESQL_POSTGRES_PASSWORD"

if [ $POSTGRES_USER != "postgres" ]; then
# IF pgpassword is not set, fail
if [ -z "${PGPASSWORD:-}" ]; then
error "If you set POSTGRES_USER to a value other than 'postgres', you need to set POSTGRESQL_POSTGRES_PASSWORD or POSTGRES_POSTGRES_PASSWORD environment variable"
error "This is because bitnami image does not make the POSTGRES_USER a superuser"
exit 1
fi

# Find "timescaledb.telemetry_level" in the postgresql.conf file
if grep -q "timescaledb.telemetry_level" /bitnami/postgresql/conf/postgresql.conf; then
# Change the owner of the initial databases to the POSTGRES_USER
psql -U postgres postgres -f "ALTER DATABASE postgres OWNER TO postgres;"
psql -U postgres template1 -f "ALTER DATABASE template1 OWNER TO postgres;"

# Change the owner of the POSTGRES_DB to the POSTGRES_USER
psql -U postgres $POSTGRES_DB -f "ALTER DATABASE $POSTGRES_DB OWNER TO postgres;"
fi
fi

0 comments on commit 0f3cb07

Please sign in to comment.