1, command line login database
There are two ways to execute the psql command directly under the system shell, but to enter the PSQL environment first and then connect to the database. Examples are given below:
(1) Direct login
Execute command: psql-h 172.16.35.179-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:
Copy Code code as follows:
Password for user username: (Enter password here)
Enter the password to enter the PSQL environment.
(2) Switching databases
Sometimes you need to switch the database in a psql environment, and then execute the following PSQL command:
Copy Code code as follows:
\c dbname username ServerIP Port
In addition to the database name, other parameters are optional, if you use the default value-as a placeholder
After you execute this command, you are prompted to enter a password.
2. View Help
Psql provides a good online help document, and the total entry command is assist, and you can enter this command to see
Copy Code code as follows:
vsb9=# Help
You are 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 for standard SQL commands is separate from the help of Psql-specific commands. Enter \? View the Psql command and find that all psql commands start with \, which is easy to distinguish from standard SQL commands.
3. Common commands
In order to facilitate memory, here the corresponding MySQL commands are listed.
(1) List all the databases
Copy Code code as follows:
Mysql:show databases
Psql: \l or \list
(2) Switching databases
Copy Code code as follows:
Mysql:use dbname
Psql: \c dbname
(3) List the data tables under the current database
Copy Code code as follows:
Mysql:show tables
Psql: \d
(4) List all fields of the specified table
Copy Code code as follows:
Mysql:show columns from table name
Psql: \d TableName
(5) View the basic situation of the specified table
Copy Code code as follows:
Mysql:describe TableName
Psql: \d+ tablename
(6) Exit Login
Copy Code code as follows:
Mysql:quit or \q.
Psql:\q