PostgreSQL - SELECT数据库

本章介绍了访问数据库的各种方法.假设我们已经在前一章中创建了一个数据库.您可以使用以下任一方法选择数据库 :

  • 数据库SQL提示符

  • 操作系统命令提示符

数据库SQL提示符

假设您已经启动了PostgreSQL客户端并且已经登陆以下SQL提示 :

 
 postgres =#

您可以查看使用 \l 的可用数据库列表,即反斜杠el命令,如下 :

postgres-# \l
                             List of databases
   Name    |  Owner   | Encoding | Collate | Ctype |   Access privileges   
-----------+----------+----------+---------+-------+-----------------------
 postgres  | postgres | UTF8     | C       | C     | 
 template0 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 template1 | postgres | UTF8     | C       | C     | =c/postgres          +
           |          |          |         |       | postgres=CTc/postgres
 testdb    | postgres | UTF8     | C       | C     | 
(4 rows)

postgres-#

现在,输入以下命令连接/选择所需的数据库;在这里,我们将连接到 testdb 数据库.

postgres=# \c testdb;
psql (9.2.4)
Type "help" for help.
You are now connected to database "testdb" as user "postgres".
testdb=#

OS命令提示符

您可以从命令提示符本身选择数据库登录数据库的时间.以下是一个简单的示例 :

psql -h localhost -p 5432 -U postgress testdb
Password for user postgress: ****
psql (9.2.4)
Type "help" for help.
You are now connected to database "testdb" as user "postgres".
testdb=#

您现在已登录到PostgreSQL testdb并准备在testdb中执行命令.要退出数据库,可以使用命令\ q.