How to provide a password for PostgreSQL's createdb non-interactively?
Asked Answered
V

1

16

I have a task in phing where before tests I drop the database if exists and create it. This is run on Jenkins. I want to do it with createdb like this:

<exec command="createdb my_database" />

The thing is that the createdb is asking me to authenticate and adding -Umy_user parameter is not a problem - the issue is that I cannot specify a password in the createdb command. And I don't want to create a role for the system user ("jenkins" in this case). Is there a solution for that ?

Varney answered 12/11, 2012 at 20:29 Comment(0)
W
32

createdb will use the PGPASSWORD environment variable if it's set, that's one simple way of providing a password in non-interactive runs.

Another option would be to set up a .pgpass file in the home directory of the unix user launching the createdb.

Whet answered 13/11, 2012 at 12:45 Comment(4)
With .pgpass, I initially tried entering a line specifying the name of the database I wanted to create. However, I still got the password prompt on createdb. Seems like the database name has to be *.Dismal
What will be used for username, if I use PGPASSWORD var? Will the username default to root?Cement
@JamesWierzba: the db username defaults to the OS username.Embank
Can you add an example how to do with .pgpass? I've .pgpass set up and properly working with psql, but it doesn't work with createdb.Interlunation

© 2022 - 2024 — McMap. All rights reserved.