Basic Command | Description |
psql -d database -U user -W | Connects to a database under a specific user |
psql -h host -d database -U user -W | Connect to a database that resides on another host |
psql -U user -h host “dbname=db sslmode=require” | Use SSL mode for the connection |
\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 |
\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 |
Start Stop and Status check
systemctl status postgresql-13.service
systemctl stop postgresql-13.service
systemctl start postgresql-13.service
PG Port Number
select * from pg_settings where name=’port’;
Create Database
CREATE DATABASE name
[ [ WITH ] [ OWNER [=] db_owner ]
[ TEMPLATE [=] template ]
[ ENCODING [=] encoding ]
[ TABLESPACE [=] tablespace ]
]
Leave a Reply