You are reading the article List Of Postgresql Commands With Examples updated in September 2023 on the website Nhahang12h.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 List Of Postgresql Commands With Examples
Start Your Free Data Science Course
Hadoop, Data Science, Statistics & others
Introduction to PostgreSQL CommandsThe PostgreSQL supports various commands which we can execute from the psql prompt. The psql prompt is an interactive interface front-end to the PostgreSQL, the same as the terminal provided with its default installation. We can use the psql prompt for writing various commands and queries interactively and execute them to the PostgreSQL for having results. It provides various meta-commands. We can give input as command-line arguments or commands from a file. If the shell command finishes without any error, then the psql returns 0. If the shell command fails because of errors like file not found or out of memory, the psql returns 1. The psql returns 2 if the server connection fails.
List of PostgreSQL Commands with ExamplesThe PostgreSQL supports various commands which we can execute from the psql prompt.
First, we will open the psql prompt, which looks like the following snapshot.
Given below are the various meta-commands:
1. Command to list all commands available in PostgreSQLThe ‘?’ command returns all of the commands available in PostgreSQL.
Illustrate the result of the ‘?’ command by using the following snapshot.
Code:
?Output:
2. Command to get the version of the PostgreSQLThe ‘SELECT VERSION()’ statement returns the version of PostgreSQL.
Illustrate the result of the ‘SELECT’ command using the following snapshot.
Code:
SELECT VERSION();Output:
3. Command to list all of the available databasesThe ‘l’ command returns all of the databases available in PostgreSQL.
Illustrate the result of the ‘l’ command by using the following snapshot.
Code:
lOutput:
4. Command to PostgreSQL database connectThis command is used to connect to the database.
Illustrate the result of the above command by using the following snapshot.
Code:
-d NewEduCBADB -U postgres –WOutput:
5. Command to switch database connection to the new databaseThe ‘c db_name [username]’ command is used to switch the connection to the new database.
Illustrate the result of the above command by using the following snapshot.
Code:
c NewEduCBADBOutput:
6. Command for listing all of the tables from the current databaseThe ‘ dt’ command returns the tables from the current database.
Illustrate the result of the ‘ dt’ command by using the following snapshot.
Code:
dtOutput:
7. Command to describe the tableThe ‘d table_name’ command is used to describe the table.
Illustrate the result of the above command by using the following snapshot.
Code:
d furnitureOutput:
8. Command to list available schemaThe ‘ dn’ command is used to list all of the schemas of the current database.
Illustrate the result of the above command by using the following snapshot.
Code:
dnOutput:
9. Command to list available functionsThe ‘ df’ command is used to list all of the functions of the current database.
Illustrate the result of the above command by using the following snapshot.
Code:
dfOutput:
10. Command to list available viewsThe ‘ dv’ command is used to list available views in the current database.
Illustrate the result of the above command by using the following snapshot.
Code:
dvOutput:
11. Command to list users and their rolesThe ‘ du’ command lists all the users with their roles.
Illustrate the result of the above command by using the following snapshot.
Code:
duOutput:
12. Command to view complete history
The ‘ s’ command is used to view the complete history in the PostgreSQL.
We can save the history in the file by using the ‘s filename’ command.
13. Command to list all SQL commandsIllustrate the result of the above command by using the following snapshot.
Code:
hOutput:
14. Command to get help on psql commandsIf we want detailed information about any statement, we can use this command.
Illustrate the result of the command by using the following snapshot.
Code:
h DROP TABLEOutput:
15. Command to get the execution time of SQL queryThe ‘timing’ command is used to get the execution time of the SQL query in the PostgreSQL.
Illustrate the result of the above command by using the following snapshot.
Code:
timingOutput:
16. Command to execute previous psql commandThe ‘g’ command is used to execute the previous command.
Code:
gOutput:
17. Command to use the psql with the text editorThe ‘e’ command opens the recently executed SQL query in a text editor. We can edit that in the query in a text editor and can run it again.
Illustrate the result of the above command by using the following snapshot.
Now we will modify the SQL query in a text editor and close the text editor.
Illustrate the result of the step by using the following snapshot.
Step 1: Modify the SQL query.
Step 2: Close the text editor.
Code:
eOutput:
18. Command to switch output format from aligned to the non-aligned column and vice-versaThe command’ a’ is used to switch the output format from aligned to the non-aligned column and vice-versa.
We will switch the output format by using the ‘ a’ command.
Code:
aOutput:
In order to illustrate the effect of ‘ a’ command for the output format change, we will execute the ‘select * from employee’ command and will have a look at the output.
Code:
select * from employee;Output:
Now we will again switch the output format by using ‘ a’ command.
Illustrate the result of the above command by using the following snapshot.
Code:
aOutput:
In order to illustrate the effect of ‘ a’ command for the output format change, we will execute the select * from employee’ command and will have a look at the output.
Code:
select * from employee;Output:
19. Command to switch output format to HTML formatThe command’ H’ is used to format the output in the HTML format.
Illustrate the result of the above command by using the following snapshot.
Code:
HOutput:
In order to illustrate the effect of ‘ H’ command for the output format change, we will execute the ‘select * from employee’ command and will have a look at the output.
Code:
select * from employee;Output:
20. Command to quit the psql in PostgreSQLThe ‘q’ command is used to quit the psql in the PostgreSQL.
Illustrate the result of the above command by using the following snapshot.
Code:
qOutput:
ConclusionIn this article, we saw how to use the PostgreSQL commands and how the PostgreSQL commands works. Also, we saw several examples of the PostgreSQL commands.
Recommended Articles
We hope that this EDUCBA information on “PostgreSQL Commands” was beneficial to you. You can view EDUCBA’s recommended articles for more information.
You're reading List Of Postgresql Commands With Examples
Update the detailed information about List Of Postgresql Commands With Examples on the Nhahang12h.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!