PostgreSQL - Psql commands
PostgreSQL, or Postgres, is an object-relational database management system that utilizes the SQL language. PSQL is a powerful interactive terminal for working with the PostgreSQL database. It enables users to execute queries efficiently and manage databases effectively.
Here, we highlight some of the most frequently used PSQL commands, detailing their functionalities to enhance your PostgreSQL experience.
Top PSQL Commands in PostgreSQL
Here are the top 22 PSQL commands that are frequently used when querying a PostgreSQL database:
Serial No. | Command | Description |
---|---|---|
1 | psql -d database -U user -W | Connects to a database under a specific user |
2 | psql -h host -d database -U user -W | Connect to a database that resides on another host |
3 | psql -U user -h host "dbname=db sslmode=require" | Use SSL mode for the connection |
4 | \c dbname | Switch connection to a new database |
5 | \l | List available databases |
6 | \dt | List available tables |
7 | \d table_name | Describe a table such as a column, type, modifiers of columns, etc. |
8 | \dn | List all schemes of the currently connected database |
9 | \df | List available functions in the current database |
10 | \dv | List available views in the current database |
11 | \du | List all users and their assign roles |
12 | SELECT version(); | Retrieve the current version of PostgreSQL server |
13 | \g | Execute the last command again |
14 | \s | Display command history |
15 | \s filename | Save the command history to a file |
16 | \i filename | Execute psql commands from a file |
17 | \? | Know all available psql commands |
18 | \h | Get help |
19 | \e | Edit command in your own editor |
20 | \a | Switch from aligned to non-aligned column output |
21 | \H | Switch the output to HTML format |
22 | \q | Exit psql shell |
Additional Information:
- The
-d
option inpsql
commands is used to state the database name.- The
-U
option specifies the database user.- The
-h
option indicates the host on which the database server resides.- The
\h ALTER TABLE
can be used to get detailed information on the ALTER TABLE statement.
Conclusion
Understanding and utilizing Psql commands can significantly enhance your efficiency when working with PostgreSQL. Here we provided an overview of essential commands to help you manage your databases more effectively. These commands are vital tools for any PostgreSQL user.
The psql interface is powerful and allows you to do many things, including running SQL statements. If you want to get started with PostgreSQL or deepen your knowledge, check out our PostgreSQL Tutorial. You can also check out the Postgre articles on our blog.