HOWTO · PostgreSQL
How to Connect to PostgreSQL With Password
This article shows various methods for connecting PostgreSQL with password. It can be via command-line, pgpass file, PGPASSWORD environment variable or the connection string.
On this page
This guide shows how to authenticate a psql connection to PostgreSQL with a password. Choose an interactive prompt when you want to enter the credential for each connection, PGPASSWORD for a command launched from a configured shell, a password file for reusable client connections, or a PostgreSQL connection URI when the application or command accepts one. The examples use placeholders; replace them with your database name, user, host, port, and password.
Choose a password method
Use the prompt for a one-off interactive login. Use PGPASSWORD when a script or shell command needs the value in its process environment. A password file can supply credentials for matching connection details without placing the password in each command. A connection URI puts the connection fields together in one argument and is useful when a tool expects a URI.
In each case, PostgreSQL still authenticates the selected database role. The client method supplies a password; it does not create a role or change that role’s password. If you are unsure which user to select, ask the database administrator or check the application’s connection settings.
Connect with psql and enter the password at the prompt
Run psql -U app_user -d app_db -W, replacing app_user and app_db with the database role and database you intend to use. The -U option selects the role, -d selects the database, and -W asks psql to prompt for a password before connecting. Enter the password when prompted; it is not part of the command text.
If the server is not on the client machine or does not use the default port, add -h db.example.com -p 5432 with the correct host and port. A prompt confirms that psql is requesting a credential, but a successful connection also depends on the role, database, server reachability, and server authentication configuration.
Set PGPASSWORD for a connection
PGPASSWORD supplies a password through the environment of the psql process. For a single Bash command, prefix it with PGPASSWORD='replace_me'; in PowerShell, set $env:PGPASSWORD = 'replace_me' before running psql. In Command Prompt, use set PGPASSWORD=replace_me in the current window. Substitute the actual password locally.
Run this Bash example to set the variable for commands started from the current shell:
export PGPASSWORD='replace_me'
Then run psql -h db.example.com -p 5432 -U app_user -d app_db from that shell, adjusting the connection values. The variable is supplied to the client process; the command shown here has not been run against a PostgreSQL server.
PostgreSQL documents client environment variables in its environment variable reference.
Store credentials in a password file
A password file has one connection record per line, with fields in this order: hostname:port:database:username:password. For example, a record might be written as db.example.com:5432:app_db:app_user:replace_me. Replace every example value with the matching connection details. PostgreSQL’s password file documentation describes the format.
Select a particular file with the passfile connection parameter. For example, run psql "host=db.example.com port=5432 dbname=app_db user=app_user passfile=/path/to/pgpass.conf". Keep the connection fields consistent with the record you want the client to use. If the client does not find a matching record, it may still prompt for a password or report an authentication failure, depending on how the connection is configured.
Connect with a PostgreSQL connection URI
A PostgreSQL URI places the role, password, host, port, and database in one connection argument. For example, run psql 'postgresql://app_user:replace_me@db.example.com:5432/app_db', replacing the placeholders with your connection values. The colon between the host and port is required. The URI form is also useful when passing a connection value to a program or library that accepts PostgreSQL URIs.
The password is one of the URI fields. Use the URI form only when the command or application accepts a PostgreSQL connection URI.
Troubleshoot connection and authentication failures
First check that -U names the intended database role and -d names the intended database. Then verify the host and port. Use the error message itself to distinguish a connection or reachability problem from an authentication rejection; prompt timing is not a reliable distinction because psql -W prompts before attempting the connection, while other client flows may prompt only after contacting the server.
If the server is reachable but rejects authentication, confirm that the password belongs to the selected role and that the server’s client authentication configuration permits this client and connection. The PostgreSQL client authentication documentation explains the server-side rules. When using a password file, check the selected passfile path and compare its host, port, database, and username fields with the command. When using PGPASSWORD, confirm that it is set in the shell or process that starts psql. These checks isolate which connection value or authentication source needs correction.