ChatGPT解决这个技术问题 Extra ChatGPT

How do I specify a password to 'psql' non-interactively?

I am trying to automate database creation process with a shell script and one thing I've hit a road block with passing a password to psql. Here is a bit of code from the shell script:

psql -U $DB_USER -h localhost -c"$DB_RECREATE_SQL"

How do I pass a password to psql in a non-interactive way?

You can use a connection url. Check this answers in stackoverflow.com/questions/3582552/postgres-connection-url.
@paulodiovani I will just add that this works with no other options - just psql Thank you.

r
rogerdpack

Set the PGPASSWORD environment variable inside the script before calling psql

PGPASSWORD=pass1234 psql -U MyUsername myDatabaseName

For reference, see http://www.postgresql.org/docs/current/static/libpq-envars.html

Edit

Since Postgres 9.2 there is also the option to specify a connection string or URI that can contain the username and password. Syntax is:

$ psql postgresql://[user[:password]@][host][:port][,...][/dbname][?param1=value1&...]

Using that is a security risk because the password is visible in plain text when looking at the command line of a running process e.g. using ps (Linux), ProcessExplorer (Windows) or similar tools, by other users.

See also this question on Database Administrators


For example in one line you can do something like: PGPASSWORD=pass1234 psql -u MyUsername myUserName
I think that this is the most convenient way for simply running an SQL script.
I can only add - add a space in the command line before the first character and the command won't be stored in bash history. Works for ubuntu/bash.
BONUS: Works for Docker: docker run -e PGPASSWORD="$(pbpaste)" --rm postgres psql -h www.example.com dbname username -c 'SELECT * FROM table;'
For those about to use this, be aware that including a password as part of a shell command will 1) display it in the process list visible by all users of the system (e.g. ps -ef), and 2) will add it to your shell's history file (e.g. .bash_history). My recommendation is to store the password in a safe file (e.g. use OS-level permissions to restrict access) and then PGPASSWORD=$(cat /<path>/to/secret.txt) ....
r
rogerdpack

From the official documentation:

It is also convenient to have a ~/.pgpass file to avoid regularly having to type in passwords. See Section 30.13 for more information.

...

This file should contain lines of the following format:

hostname:port:database:username:password

The password field from the first line that matches the current connection parameters will be used.


Thanks, I am aware of pgpass, but this doesn't solve the issue - I need a self-contained bash script to operate over the database, hence my question about passing info to psql via command line.
I think your only option is to set up a .pgpass file that your bash script has access to. Or don't use passwords at all--you could set up another form of authentication, such as ident, or using SSL certificates.
That's what I feared :) Thanks for the info!
Another option might be to use expect. But I really hate expect :)
Don't forget to remove group and other user permission to read, write, execute the .pgpass file! Run chmod go-rwx .pgpass
A
AdrieanKhisbe

in one line: export PGPASSWORD='password'; psql -h 'server name' -U 'user name' -d 'base name' -c 'command' with command a sql command such as "select * from schema.table"

or more readable: export PGPASSWORD='password' psql -h 'server name' -U 'user name' -d 'base name' \ -c 'command' (eg. "select * from schema.table")


The one line can be slightly simplified to: PGPASSWORD='password' psql .... which also has the benefit of the variable not being accessible after the command is done.
export PGPASSWORD=YourNewPassword worked for me over other variations.
export PGPASSWORD sounds like a really bad idea
This will save the password in your bash history ~/.bash_history file (unless you carefully always add a preceding space), and also export the password to your current environment FWIW :|
There's no need to export PGPASSWORD.
P
Peter Mortensen

I tend to prefer passing a URL to psql:

psql "postgresql://$DB_USER:$DB_PWD@$DB_SERVER/$DB_NAME"

This gives me the freedom to name my environment variables as I wish and avoids creating unnecessary files.

This requires libpq. The documentation can be found here.


can you use something like this with pg_restore? Thanks!
@ccalderon911217 Apparently you can: stackoverflow.com/a/28359470/1388292
@JacquesGaudin yep tested myself! Thank you!
B
Brian Burns

On Windows:

Assign value to PGPASSWORD: C:\>set PGPASSWORD=pass Run command: C:\>psql -d database -U user

Ready

Or in one line,

set PGPASSWORD=pass&& psql -d database -U user

Note the lack of space before the && !


I tried that, and it did not work. Still being asked for a password.
@antipattern You must pass option -w also.
PGPASSWORD=xxxx psql -U username -d database -w -c "select * from foo;" works.
In Powershell, you can do this: $env:PGPASSWORD=pass; psql -d database -U user
+1000 for the comment about no space before the && :)
j
jonsca

This can be done by creating a .pgpass file in the home directory of the (Linux) User. .pgpass file format:

<databaseip>:<port>:<databasename>:<dbusername>:<password>

You can also use wild card * in place of details.

Say I wanted to run tmp.sql without prompting for a password.

With the following code you can in *.sh file

echo "192.168.1.1:*:*:postgres:postgrespwd" > $HOME/.pgpass
echo "` chmod 0600 $HOME/.pgpass `"

echo " ` psql -h 192.168.1.1 -p 5432  -U postgres  postgres  -f tmp.sql `        

What's the point of echo "` chmod 0600 $HOME/.pgpass `"? How about just chmod 0600 $HOME/.pgpass ?
P
Peter Mortensen

An alternative to using the PGPASSWORD environment variable is to use the conninfo string according to the documentation:

An alternative way to specify connection parameters is in a conninfo string or a URI, which is used instead of a database name. This mechanism give you very wide control over the connection.

$ psql "host=<server> port=5432 dbname=<db> user=<user> password=<password>"

postgres=>

I often use this approach as it seems more readable, but for the sake of security, having the password in the command is not a brilliant idea, as it can be read with a simple ps a command by any (non-root) user
R
Raj Verma

If its not too late to add most of the options in one answer:

There are a couple of options:

set it in the pgpass file. link

set an environment variable and get it from there: export PGPASSWORD='password' and then run your psql to login or even run the command from there: psql -h clustername -U username -d testdb On windows you will have to use "set" : set PGPASSWORD=pass and then login to the psql bash. Pass it via URL & env variable: psql "postgresql://$USER_NAME:$PASSWORD@$HOST_NAME/$DB_NAME"


R
Rob

Added content of pg_env.sh to my .bashrc:

cat /opt/PostgreSQL/10/pg_env.sh

#!/bin/sh
# The script sets environment variables helpful for PostgreSQL

export PATH=/opt/PostgreSQL/10/bin:$PATH
export PGDATA=/opt/PostgreSQL/10/data
export PGDATABASE=postgres
export PGUSER=postgres
export PGPORT=5433
export PGLOCALEDIR=/opt/PostgreSQL/10/share/locale
export MANPATH=$MANPATH:/opt/PostgreSQL/10/share/man

with addition of (as per user4653174 suggestion)

export PGPASSWORD='password'

P
Promise Preston

Just to add more clarity.

You can assign the password to the PGPASSWORD variable.

So instead of the below which will require you to type the password:

psql --host=aurora-postgres.cluster-fgshdjdf.eu-west-1.rds.amazonaws.com --port=5432 --user=my_master_user --password --dbname=postgres

We will replace the --password flag with PGPASSWORD=QghyumjB3ZtCQkdf. So it will be:

PGPASSWORD=QghyumjB3ZtCQkdf psql --host=aurora-postgres.cluster-fgshdjdf.eu-west-1.rds.amazonaws.com --port=5432 --user=my_master_user --dbname=postgres

This way you will not be required to type the password.