How can I call psql so that it doesn\'t prompt for a password?
This is what I have:
psql -Umyuser < myscript.sql
This also works for other postgresql clis for example you can run pgbench in non-interactive mode.
export PGPASSWORD=yourpassword
/usr/pgsql-9.5/bin/pgbench -h $REMOTE_PG_HOST -p 5432 -U postgres -c 12 -j 4 -t 10000 example > pgbench.out 2>&1 &
You can add this command line at the begining of your script:
set PGPASSWORD=[your password]
I find, that psql show password prompt even you define PGPASSWORD variable, but you can specify -w option for psql to omit password prompt.
Given the security concerns about using the PGPASSWORD environment variable, I think the best overall solution is as follows:
There are a couple points of note here. Step 1 is there to avoid mucking with the user's ~/.pgpass file that might exist. You also must make sure that the file has permissions 0600 or less.
Some have suggested leveraging bash to shortcut this as follows:
PGPASSFILE=<(echo myserver:5432:mydb:jdoe:password) psql -h myserver -U jdoe -p 5432 mydb
This uses the <() syntax to avoid needing to write the data to an actual file. But it doesn't work because psql checks what file is being used and will throw an error like this:
WARNING: password file "/dev/fd/63" is not a plain file
8 years later...
On my mac, I had to put a line into the file
~/.pgpass
like:
<IP>:<PORT>:<dbname>:<user>:<password>
Also see:
https://www.postgresql.org/docs/current/libpq-pgpass.html
https://wiki.postgresql.org/wiki/Pgpass
There are several ways to authenticate to PostgreSQL. You may wish to investigate alternatives to password authentication at https://www.postgresql.org/docs/current/static/client-authentication.html.
To answer your question, there are a few ways provide a password for password-based authentication. The obvious way is via the password prompt. Instead of that, you can provide the password in a pgpass file or through the PGPASSWORD
environment variable. See these:
There is no option to provide the password as a command line argument because that information is often available to all users, and therefore insecure. However, in Linux/Unix environments you can provide an environment variable for a single command like this:
PGPASSWORD=yourpass psql ...