Im trying to create a Dockerfile from the postgres image. The repo says that initialization should be handled by placing a shell script in /docker-entrypoint-initdb.d/. I pu
If you want to prevent PostgreSQL from being accessible to users before whatever setup you need to perform is done, start it with only loopback access or only a unix socket, do your initialisation, then restart it for general access.
I don't speak Docker, but if you were doing this in a regular environment you'd do something like:
mkdir -p /db/temp_socket
chown -r postgres:postgres /db
PGHOST=/db/temp_socket pg_ctl -D /path/to/datadir -o "-c listen_addresses='' -c unix_socket_directories='/db/temp_socket'" -l "/db/dbsetup.log" -w start
# Do your work
PGHOST=/db/temp_socket psql -f some_script
PGHOST=/db/temp_socket pg_ctl -D /path/to/datadir -m fast -w stop
pg_ctl -D /path/to/datadir -w start ...normalstartupoptionsblah...
i.e. start PostgreSQL not listening on any TCP/IP sockets, and with a non-default unix_socket_directories. Do your setup. Then restart it with the default (or configured) unix_socket_directories and listen_addresses once it's ready for general access.
Instead of this you could:
pg_hba.conf to only allow access from your setup user / only on the loopback address / etcpg_hba.conf with the production onepg_ctl reload or SELECT pg_reload_conf() to load the new settings and allow general access... however this will permit applications to connect then reject their authentication during the setup stage; that may not be what you want, and not all applications cope with this correctly.