Automating PostgreSQL Database Connections in Scripts: A Guide to Password Management

Jason Anderson
2 min readFeb 15, 2024

--

Postgresql
Postgresql

To connect to a PostgreSQL database with a password, you usually use the psql command-line tool. The basic format of the connection command is as follows:

psql -h [hostname] -p [port] -U [username] -d [database] -W

Where:

  • -h is used to specify the database server's hostname or IP address.
  • -p is used to specify the database server's port number (default is 5432).
  • -U is used to specify the username to connect to the database.
  • -d is used to specify the name of the database to connect to.
  • -W indicates that it will prompt for a password upon connection.

For example, if you want to connect to a database named mydb running on localhost with the username user, you can use the following command:

psql -h localhost -p 5432 -U user -d mydb -W

After executing this command, the system will prompt you to enter the password.

Note, for security reasons, it is recommended not to include the password directly in the command line but to use the -W parameter to let psql prompt you for the password, or manage the password through a configuration file.

In a script that executes in a loop and connects to the PostgreSQL database, it’s indeed inconvenient to manually enter the password each time. A more automated method is to use an environment variable or a .pgpass file to avoid having to enter the password for each connection.

Using the .pgpass File

  1. Create a file named .pgpass in the user's home directory (on Windows, the file is named pgpass.conf and is usually located in the user's application data directory, for example, C:\Users\Username\AppData\Roaming\postgresql\pgpass.conf).
  2. Add the database connection information to the .pgpass file in the following format:
  • hostname:port:database:username:password
  1. For example:
  • localhost:5432:mydb:user:password
  1. Make sure the .pgpass file's permission is set to be readable and writable by the owner only (on Linux or macOS, you can set this with the command chmod 600 ~/.pgpass).
  2. In your script, you can now call the psql command directly without the -W parameter, as psql will automatically read the password from the .pgpass file.

Using Environment Variables

Another method is to set the PGPASSWORD environment variable before executing the script. This is convenient for temporary scripts or one-time tasks, but note that this method might pose a security risk since environment variables could potentially be read by other processes running on the same system.

export PGPASSWORD='yourpassword'

Then, in the script, you can directly use the psql command without needing the -W parameter.

For example:

export PGPASSWORD='mypassword'
psql -h localhost -p 5432 -U user -d mydb

After the script finishes executing, for security reasons, you might want to unset the PGPASSWORD environment variable:

unset PGPASSWORD

Please choose the method that best suits your security policies and script execution environment.

--

--