Open In App

PostgreSQL – Psql commands

Last Updated : 28 Sep, 2021
Like Article

Psql is an interactive terminal to work with the PostgreSQL database. It is used to query data from the PostgreSQL database server faster and more effectively. In this article, we will look into some of the most frequently used Psql commands. 

The below table provides with the frequently used Psql commands: 

Command Description Additional Information
psql -d database -U user -W Connects to a database under a specific user -d: used to state the database name 
-U:used to state the database user
psql -h host -d database -U user -W Connect to a database that resides on another host -h: used to state the host 
-d: used to state the database name 
-U:used to state the database user
psql -U user -h host “dbname=db sslmode=require” Use SSL mode for the connection -h: used to state the host 
-U:used to state the database user
\c dbname Switch connection to a new database  
\l List available databases  
\dt List available tables  
\d table_name Describe a table such as a column, type, modifiers of columns, etc.  
\dn List all schemes of the currently connected database  
\df List available functions in the current database  
\dv List available views in the current database  
\du List all users and their assign roles  
SELECT version(); Retrieve the current version of PostgreSQL server  
\g Execute the last command again  
\s Display command history  
\s filename Save the command history to a file  
\i filename Execute psql commands from a file  
\? Know all available psql commands  
\h Get help Eg:to get detailed information on ALTER TABLE statement use the \h ALTER TABLE
\e Edit command in your own editor  
\a Switch from aligned to non-aligned column output  
\H Switch the output to HTML format  
\q Exit psql shell  


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads