I'm trying to automate initializing a postgresql data directory, with a user and a default database pre-created. I know I can do this by running postgresql backgrounded and using createdb
and createuser
commands, but I really want to avoid that as this is for a docker container and I just want postgres to be running in the foreground once done.
In MySQL I use --init-file /path/to/bootstrap.sql
, which runs the statements in the file before the server accepts connections. You can also use --bootstrap
, which temporarily spins up mysqld and runs statements from STDIN.
I can't find an equally straightforward way to do this in Postgres. I can see a --boot
option to postgres
, but when I try and use it I just get "invalid command line arguments", so I must be doing something wrong.
echo "CREATE USER bob WITH PASSWORD 'secure';" | postgres --boot postgres -D data
postgres: invalid command-line arguments
postgres -D /path/to/data/dir
. It sits in the foreground, which is exactly what Docker containers need to do :)