For beginners of PostgreSql, learn the basic commands and summary of postgresql

Source: Internet
Author: User
Tags mysql commands psql

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.