Skip to content
Related Articles

Related Articles

PostgreSQL – Psql commands
  • Last Updated : 28 Aug, 2020

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:

CommandDescriptionAdditional Information
psql -d database -U user -WConnects 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 -WConnect 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 dbnameSwitch connection to a new database
\lList available databases
\dtList available tables
\d table_nameDescribe a table such as a column, type, modifiers of columns, etc.
\dnList all schemas of the currently connected database
\dfList available functions in the current database
\dvList available views in the current database
\duList all users and their assign roles
SELECT version();Retrieve the current version of PostgreSQL server
\gExexute the last command again
\sDisplay command history
\s filenameSave the command history to a file
\i filenameExecute psql commands from a file
\?Know all available psql commands
\hGet helpEg:to get detailed information on ALTER TABLE statement use the \h ALTER TABLE
\eEdit command in your own editor
\aSwitch from aligned to non-aligned column output
\HSwitch the output to HTML format
\qExit psql shell
My Personal Notes arrow_drop_up
Recommended Articles
Page :