Psql Commands
Connect to Psql
• Connect to Specific Database with user and password
Syntax: psql -d database -U user –W (-d =Database, -U = User, -W =
Password)
• Connect to Database on a different host/machine.
Syntax: psql -h host -d database -U user –W
• Connect using SSL Mode
Syntax: psql -U user -h host "dbname=db sslmode=require"
Psql Commands
• Switch connection to a new database
postgres=# \c test1
You are now connected to database "test1" as user "postgres".
• List available databases
postgres=# \l
• List available tables
postgres=# \ dt
• List users and their roles(+ to get more info)
postgres=# \du
• List available sequence(+ to get more info)
postgres=# \ds
• Execute the previous command
postgres=# \g
• Command history
postgres=# \s
• Save Command History to file:
postgres=# \s filename
• Get help on psql commands
postgres=# \?
• Turn on\off query execution time
postgres=# \timing
• Edit statements in editor
postgres=# \e
• Edit Functions in editor
postgres=# \ef
• set output from non-aligned to aligned column output.
postgres=# \a
• Formats output to HTML format.
postgres=# \H
• Connection Information
postgres=# \conninfo
• Quit psql
postgres=# \q
Psql File Operations
• Run sql statements from a file.
psql -d test1 -U test1 -f [Link] ( command line)
• Send the output to a file.
postgres=# \o <filename>
• Save query buffer to filename.
postgres=# \w filename
• Turn off auto commit on session level
\set AUTOCOMMIT off