knktc's Notes

python, cloud, linux...

0%

Run pg_dump Without Entering a Password

I recently needed to write a simple scheduled backup script for PostgreSQL, and quickly noticed that pg_dump wanted an interactive password every time. That obviously does not fit well with automation, so I looked up a couple of ways around it.

There are two common solutions:

  1. Use an environment variable and assign the database password to PGPASSWORD:
1
export PGPASSWORD="$put_here_the_password"
  1. Create a .pgpass file in the user’s home directory and store the connection credentials there:
1
localhost:5432:mydbname:postgres:mypass

After that, set the file permissions to 600, otherwise PostgreSQL may ignore it.

Both methods allow password-protected commands such as pg_dump to run without prompting for input.

Reference:

http://www.postgresql.org/docs/current/static/libpq-pgpass.html

如果我的文字帮到了您,那么可不可以请我喝罐可乐?