0% found this document useful (0 votes)
7 views3 pages

Essential Psql Commands Guide

The document provides a comprehensive guide on using Psql commands for connecting to databases, managing connections, and executing SQL statements. It includes syntax for connecting to specific databases, listing databases and tables, and performing file operations. Additionally, it covers command history, output formatting, and session management features.

Uploaded by

Oracle Forensics
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views3 pages

Essential Psql Commands Guide

The document provides a comprehensive guide on using Psql commands for connecting to databases, managing connections, and executing SQL statements. It includes syntax for connecting to specific databases, listing databases and tables, and performing file operations. Additionally, it covers command history, output formatting, and session management features.

Uploaded by

Oracle Forensics
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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

You might also like