psql -d dbname -U username -h host "sslmode=require" -W
if you omit the user parameter, the current user is assumed.
\c dbname username
To list all database in the current PostgreSQL database server
\list
\l # shortcut
To list all tables in the current database
\dt
To describe a table such as a column, type, modifiers of columns, etc…
\d table_name
To list all schema of the currently connected database,
\dn
To list all functions in the current database
\df
To list all views in the current database
\dv
To list all users and their assign roles
\du
To retrieve the current version of PostgreSQL server
select version();
Now, you want to save time typing the previous command again, you can use \g
command to execution the previous command
\g
To display command history
\s
if you want to save the command history to a file , you need to specify the file name followed the \s
command as follows:
\s filename
\i filename
To know all available psql command, you use the \?
command:
\?
To get help on specific PostgreSQL statement, you use the following command:
\h alter table
To turn on query execution time, you use the following command:
\timing
you use the same command to turn it off.
It is very handy if you can type the command in your favorite editor. To do this in psql, you \e
command. After issuing the command, psql will open the text editor defined by your EDITOR environment variable and place the most recent command that you entered in psql into the editor.
\e
After you type the command in the editor, save it and close the editor, psql will execute the command and return the result.
It is more useful when you edit a function in the editor:
\ef [function name]
Switch output options
psql supports some type of output format and allows your to customize how to output is formatted on fly:
\a # command switches from aligned to non-aligned column output
\H # command formats the output to HTML format.
\q