I have a shell script which runs on deployment and I have these lines:
# Database
createdb $DBNAME
createuser -D -A $DBNAME
However, in my logs I get this error:
Would anyone mind telling me what is going wrong here and how I can correct my lines. Surely root should have permission to do this?
the best way to do this, especially if you want your script to be portable, is:
su --login postgres --command "createdb $DBNAME"
this should be safer, more secure, and more portable than using -U. i do it this way in all my posgreSQL scripts. you might find it a useful technique. obviously it still needs to be run as a root user (e.g. with sudo).
If your DB is secured, you need to connect as a DB user, not as a user of the OS. For example:
createdb -U dbrootuser -W $DBNAME
See this link for full syntax
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With