psql 带上密码


postgresql-scripting-psql-execution-with-password

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:

  • https://www.postgresql.org/docs/9.0/static/libpq-pgpass.html
  • https://www.postgresql.org/docs/9.0/interactive/libpq-envars.html

Command-line 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 ...psql -h 192.168.1.8 -p 5432 -U lg -d mydb -c "select * from myschema.table"