'Install postgres extension into Bitnami container as superuser on initial startup

I am using the Bitnami Postgres Docker container and noticed that my ORM which uses UUIDs requires the uuid-ossp extension to be available. After some trial and error I noticed that I had to manually install it using the postgres superuser since my custom non-root user created via the POSTGRESQL_USERNAME environment variable is not allowed to execute CREATE EXTENSION "uuid-ossp";.

I'd like to know what a script inside /docker-entrypoint-initdb.d might look like that can execute this command into the specific database, to be more precise to automate the following steps I had to perform manually:

psql -U postgres // this requires interactive password input
\c target_database
CREATE EXTENSION "uuid-ossp";


Solution 1:[1]

I think that something like this should work

PGPASSWORD=$POSTGRESQL_POSTGRES_PASSWORD psql -U postgres // this requires interactive password input
\c target_database
CREATE EXTENSION "uuid-ossp";

Solution 2:[2]

If you want to do it on startup you need to add a file to the startup scripts. Check out the config section of their image documentation: https://hub.docker.com/r/bitnami/postgresql-repmgr/

If you're deploying it via helm you can add your scripts in the postgresql.initdbScripts variable. values.yaml.

If the deployment is already running, you'll need to connect as the repmgr user not the Postgres user you created. That's default NOT a superuser for security purposes. This way most of your connections are not privileged.

For example I deployed bitnami/postgresql-ha via helm to a k8s cluster in a namespace called "data" with the release name "prod-pg". I can connect to the database with a privileged user by running

export REPMGR_PASSWORD=$(kubectl get secret --namespace data prod-pg-postgresql-ha-postgresql -o jsonpath="{.data.repmgr-password}" | base64 --decode)

kubectl run prod-pg-postgresql-ha-client \
    --rm --tty -i --restart='Never' \
    --namespace data \
    --image docker.io/bitnami/postgresql-repmgr:14 \
    --env="PGPASSWORD=$REPMGR_PASSWORD"  \
    --command -- psql -h prod-pg-postgresql-ha-postgresql -p 5432 -U repmgr -d repmgr

This drops me into an interactive terminal

$ ./connect-db.sh 
If you don't see a command prompt, try pressing enter.

repmgr=#

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Javier J. Salmeron Garcia
Solution 2 lwileczek