Search code examples
linuxpostgresqlmonit

Create postgres user during boot


I'm running a bash file from monit during boot, that bash file starts my postgres server.

if my database directory is not present, I do:

1- initdb (postgresql/data/) su - edge -c '/usr/bin/initdb -D ${DBDIR}'

2- copy modified pg_hba.conf and postgresql.conf files to (postgresql/data/)

3- start my server su - edge -c " /usr/bin/pg_ctl -w -D ${DBDIR} -l logfile start"

4- postgres createuser - su - $User -c '${DBDIR} -e -s postgres'

after the execution of the bash file postgresql/data/ is created files are copied server is started, but user is not created so I cannot access my database

error : /usr/bin/psql -U postgres psql: FATAL: role "postgres" does not exist


Solution

  • I can't decipher your step #4, but the reason why the postgres role does not exist is because the step #1 is run by a user edge and it doesn't ask for the creation of a postgres role through -U, so it creates an egde role as superuser instead.

    Per initdb documentation:

    -U username
    --username=username

    Selects the user name of the database superuser. This defaults to the name of the effective user running initdb. It is really not important what the superuser's name is, but one might choose to keep the customary name postgres, even if the operating system user's name is different.

    Either do initdb -U postgres, or if you prefer a superuser named edge, keep it like this but start psql with psql -U edge, or set the PGUSER environment variable to edge to avoid typing that each time.