psql

From wikieduonline
Revision as of 14:37, 12 September 2022 by Ant (talk | contribs) (→‎Examples)
Jump to navigation Jump to search

wikipedia:psql command line tool to connect and execute queries against a PostgreSQL (port 5432) or Amazon Redshift database (port 5439).

Examples

Connect to remote database:

  • psql --host=mypostgresql.c6c8mwvfdgv0.us-west-2.rds.amazonaws.com --port=5432 --username=awsuser --password --dbname=mypgdb
  • psql -h localhost -U YOUR_USERNAME DB_NAME
  • psql -h <REMOTE HOST> -p <REMOTE PORT> -U <DB_USER> <DB_NAME>

Other examples

psql
psql: FATAL:  role "root" does not exist
su - postgres && psql
psql (10.12 (Ubuntu 10.12-0ubuntu0.18.04.1))
Type "help" for help.
postgres=#
sudo -u postgres psql
could not change directory to "/root": Permission denied
psql (12.6 (Ubuntu 12.6-0ubuntu0.20.04.1))
Type "help" for help.

Execution with errors

psql
Warning: No existing cluster is suitable as a default target. Please see man pg_wrapper(1) how to specify one.
Error: You must install at least one postgresql-client-<version> package
FATAL: no pg_hba.conf entry for host "1x.1xx.2xx.1xx", user "your_username", database "your_db_name", SSL off
psql
psql: error: could not connect to server: No such file or directory
        Is the server running locally and accepting
        connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?

macOS

psql
psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: No such file or directory
Is the server running locally and accepting connections on that socket?
Solution: Check if your local database is runnning or provide remote server to connect using --host or service options defined in your $HOME/.pg service.conf file.
psql service=pro
psql: error: could not connect to server: Connection refused
        Is the server running on host "your_rds_instance.eu-central-1.rds.amazonaws.com" (7.123.109.22) and accepting
        TCP/IP connections on port 5433 ?
psql
Password for user YOUR_USERNAME:
psql: error: FATAL:  password authentication failed for user "YOUR_USERNAME"
FATAL:  no pg_hba.conf entry for host "xx.xx.xx.xx", user "YOUR_USERNAME", database "YOUR_DATABASE", SSL off

Options

  • Port (-p): psql -p 5432

Meta-commands

\du, \dg, \ddp, \dp, \list, \dn, \dt, \c, \l

Operations

Related terms

See also

Advertising: