tianon / docker-postgres-upgrade

a PoC for using "pg_upgrade" inside Docker -- learn from it, adapt it for your needs; don't expect it to work as-is!
https://hub.docker.com/r/tianon/postgres-upgrade/
MIT License
1.04k stars 114 forks source link

Does the operation of `exec gosu postgres "$BASH_SOURCE" "$@"` overlap with `exec "$@"`? #87

Closed cobolbaby closed 6 months ago

tianon commented 6 months ago

This line:

https://github.com/tianon/docker-postgres-upgrade/blob/65ab4e308514a3139bca8ac94bdabfba234a29f5/docker-upgrade#L13

boils down to essentially "run this script with the same arguments, but as user postgres instead of root" (note that it's in an if [ "$(id -u)" = '0' ]; then block, which is "if we are root")

See also https://github.com/docker-library/postgres/blob/1424abf76f421d6f7bf933d9e42bbbed866fae3a/16/bookworm/docker-entrypoint.sh#L311-L314

cobolbaby commented 6 months ago

I still don't quite understand this detail. If executed with the root user, after running gosu postgres bash pg_upgrade --link once, does it continue with operations like initdb or exec "$@"?

tianon commented 6 months ago

Flattened and without conditionals, the flow is something like:

# as root
mkdir -p "$PGDATAOLD" "$PGDATANEW"
chmod 700 "$PGDATAOLD" "$PGDATANEW"
chown postgres .
chown -R postgres "$PGDATAOLD" "$PGDATANEW"

# as postgres
PGDATA="$PGDATANEW" eval "initdb $POSTGRES_INITDB_ARGS"
exec pg_upgrade ...
cobolbaby commented 6 months ago

Copy that.