Providing password for "psql" in shell scripts

IMHO, PostgreSQL > MySQL, but it has its own quirks.

For example, running SQL statements using psql is a bit complicated if you do not want to be prompted for a password. Research on the topic is made a bit difficult by the fact that the answers are provided in the "libpq" section of the PostgreSQL manual, rather than the manpage for psql.

The upside is that I think it works with createdb, createuser and other commands (did not test that though).

There are a few recommended ways of doing this.
However it can also be done using environmental variable in a shell script:

PGPASSWORD="your_password" psql -U postgres -c "select 1;"

Such approach is carries a security risk of someone seeing the password, but can be quite useful in a situation where you need to run a list of commands and do not care about security.

Information on PGPASSWORD and other environmental variables is found here: http://www.postgresql.org/docs/current/static/libpq-envars.html

No comments:

Post a Comment