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=# 

操作系统命令提示符


你可以在登录数据库时从命令提示符中选择你的数据库。下面是一个简单的例子:

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。