PostgreSql is a must for beginners

  • 2020-05-06 11:54:54
  • OfStack

1, command line login database

There are two ways: one is to directly execute the psql command under system shell; Instead, you enter the psql environment and then connect to the database. Examples are given below:

(1) directly log in

Execute command: psql-h 172.16.35.179-U username-d dbname, username is the database user name, dbname is the database name to connect to, after execution prompt for the password as follows:


Password for user username: (enter password here)

Enter your password and you're ready to enter the psql environment.

(2) switch database

Sometimes you need to switch databases in an psql environment, and execute the following psql command:


\c dbname username serverIP port

All of the parameters except the database name are optional and can be used if the default value is used - as the placeholder

After executing this command, you are also prompted for the password.

2, see help

psql provides excellent online help documentation, and the general entry command is help. Enter this command to see


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

As you can see, the help from the standard SQL command is separate from the help from the psql specific command. Input \ & # 63; If you look at the psql command, you'll see that all the psql commands begin with \, which is easy to distinguish from the standard SQL command.
3. Common command
The mysql command is listed here for easy recall.
(1) list all databases

mysql: show databases
psql: \l or \list

(2) switch database

mysql: use dbname
psql: \c dbname

(3) list the data table
under the current database

mysql: show tables
psql: \d

(4) lists all fields
for the specified table

mysql: show columns from table name
psql: \d tablename

(5) view the base case for the specified table

mysql: describe tablename
psql: \d+ tablename

(6) log out of

mysql: quit or \q
psql:\q


Related articles: