For beginners of PostgreSql, learn the basic commands and summary of postgresql
1. log on to the database through the command line
There are two methods: one is to directly execute the psql command under the system shell; the other is to first enter the psql environment and then connect to the database. The following are examples:
(1) log on directly
Run the command psql-h 172.16.35.179-U username-d dbname, where username is the database user name and dbname is the name of the database to be connected. After the command is executed, the system prompts you to enter the password as follows:
Copy codeThe Code is as follows:
Password for user username: (enter the Password here)
Enter the password to enter the psql environment.
(2) Switch Database
Sometimes you need to switch the database in the psql environment and execute the following psql command:
Copy codeThe Code is as follows:
\ C dbname username serverIP port
Other parameters except the database name are optional. If you use the default value, you can use-as a placeholder.
After executing this command, you are prompted to enter the password.
2. View help
Psql provides a good online help document. The total entry command is help. Enter this command to see
Copy codeThe Code is as follows:
Vsb9 = # help
You are using psql, the command-line interface to PostgreSQL.
Type: \ copyright for distribution terms
\ H for help with SQL commands
\? For help with psql commands
\ G or terminate with semicolon to execute query
\ Q to quit
We can see that the help of standard SQL commands and the help of psql-specific commands are separated. Input \? View the psql command, and you will find that all psql commands start with \, which is easy to distinguish from standard SQL commands.
3. Common commands
For ease of memory, the corresponding mysql commands are also listed here.
(1) list all databases
Copy codeThe Code is as follows:
Mysql: show databases
Psql: \ l or \ list
(2) Switch Database
Copy codeThe Code is as follows:
Mysql: use dbname
Psql: \ c dbname
(3) List data tables in the current database
Copy codeThe Code is as follows:
Mysql: show tables
Psql: \ d
(4) list all fields in the specified table
Copy codeThe Code is as follows:
Mysql: show columns from table name
Psql: \ d tablename
(5) view the basic information of the specified table
Copy codeThe Code is as follows:
Mysql: describe tablename
Psql: \ d + tablename
(6) log out
Copy codeThe Code is as follows:
Mysql: quit or \ q
Psql: \ q