Postgresql: Scripting psql execution with password
You may wish to read a summary of the ways to authenticate to PostgreSQL.
To answer your question, there are several ways provide a password for password-based authentication:
Via the password prompt. Example:
psql -h uta.biocommons.org -U fooPassword for user foo:
In a pgpass file. See libpq-pgpass. Format:
<host>:<port>:<database>:<user>:<password>
With the
PGPASSWORD
environment variable. See libpq-envars. Example:export PGPASSWORD=yourpasspsql ...# Or in one line for this invocation only:PGPASSWORD=yourpass psql ...
In the connection string The password and other options may be specified in the connection string/URI. See app-psql. Example:
psql postgresql://username:password@dbmaster:5433/mydb?sslmode=require
You can add this command line at the begining of your script:
set PGPASSWORD=[your password]