15

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 ?

Nowaker
  • 12,154
  • 4
  • 56
  • 62
karolsojko
  • 711
  • 1
  • 8
  • 27

1 Answers1

31

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.

Daniel Vérité
  • 58,074
  • 15
  • 129
  • 156
  • 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 `*`. – S. Kirby Sep 09 '16 at 18:50
  • What will be used for username, if I use `PGPASSWORD` var? Will the username default to `root`? – James Wierzba Jan 24 '19 at 01:03
  • @JamesWierzba: the db username defaults to the OS username. – Daniel Vérité Jan 24 '19 at 09:19
  • 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`. – user Feb 23 '22 at 23:28