1 Command line logon database
There are two ways to execute the psql command directly under the system shell, instead of entering the PSQL environment before connecting to the database. Examples are given below:
(1) Direct login
Execute command:psql-h 192.168.1.120-u username-d dbname , where username is the database user name, dbname is the name of the database to be connected, prompt to enter the password after execution is as follows: Password for User username: (Enter password here)
Enter the PSQL environment after entering the password.
(2) Switch database
Sometimes it is necessary to switch the database in Psql environment, at this time execute the following PSQL command:
\c dbname username ServerIP Port
In addition to the database name, other parameters are optional, if you use the default value can be used-as a placeholder
After executing this command, you are also prompted for a password.
2 View Help
Psql provides a good online help document, the total Entry command is the helper, enter this command to see
vsb9=# help is using Psql, the command-line interface to PostgreSQL. Type: \copyright For distribution Terms \h to 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 for standard SQL commands is separate from the Help for Psql-specific commands. Enter \ To view the Psql command, 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 command is also listed here.
(1) List all the databases
Mysql:show databases
Psql: \l or \list
(2) Switch database
Mysql:use dbname
Psql: \c dbname
(3) List the data tables under the current database
Mysql:show tables
Psql: \d
(4) List all fields of the specified table
Mysql:show columns from table name
Psql: \d TableName
(5) View the basic situation of the specified table
Mysql:describe TableName
Psql: \d+ tablename
(6) Sign out
Mysql:quit or \q
Psql:\q
Reference: PostgreSQL 8.1 Chinese documents
PostgreSQL Getting Started command