snippetsqlMinor
How to set password for PostgreSQL database with a script?
Viewed 0 times
postgresqlscriptwithpassworddatabaseforhowset
Problem
In a bash script I'm writing, I need to create a database and set a password that is generated automatically by my script.
The problem is I can't find a way to set up the password without user interaction.
I need something like this:
Any ideas?
The problem is I can't find a way to set up the password without user interaction.
I need something like this:
$ createdb mydb -p $(cat password_file)Any ideas?
Solution
From the documentation:
There is no effective difference between creating databases via this
utility and via other methods for accessing the server.
This means that you can write a script which issues a command like
Where
This means that you will have a user for every database created.
(At the same time I have a mild suspicion that your problem is what riouj answered.)
createdb is a wrapper around the SQL command CREATE DATABASE.There is no effective difference between creating databases via this
utility and via other methods for accessing the server.
This means that you can write a script which issues a command like
psql -f createdb.sql -v passwd=$passwd -v user=$userWhere
createdb.sql contains something likeCREATE ROLE :user WITH LOGIN PASSWORD :passwd;
CREATE DATABASE mydb OWNER :user;
GRANT {some privileges on the newly created database and/or its objects};This means that you will have a user for every database created.
(At the same time I have a mild suspicion that your problem is what riouj answered.)
Code Snippets
psql -f createdb.sql -v passwd=$passwd -v user=$userCREATE ROLE :user WITH LOGIN PASSWORD :passwd;
CREATE DATABASE mydb OWNER :user;
GRANT {some privileges on the newly created database and/or its objects};Context
StackExchange Database Administrators Q#44678, answer score: 9
Revisions (0)
No revisions yet.