Postgresql 简明教程

PostgreSQL - SELECT Database

本章介绍了访问数据库的各种方法。假定我们在上一章中已经创建了一个数据库。可以使用以下方法之一选择数据库:-

This chapter explains various methods of accessing the database. Assume that we have already created a database in our previous chapter. You can select the database using either of the following methods −

  1. Database SQL Prompt

  2. OS Command Prompt

Database SQL Prompt

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

Assume you have already launched your PostgreSQL client and you have landed at the following SQL prompt −

postgres=#

可以使用 \l (即反斜杠el命令)按如下方式检查可用的数据库列表:-

You can check the available database list using \l, i.e., backslash el command as follows −

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数据库。

Now, type the following command to connect/select a desired database; here, we will connect to the testdb database.

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

OS Command Prompt

您可以在登录到数据库时从命令提示符本身选择您的数据库。以下是简单的示例:-

You can select your database from the command prompt itself at the time when you login to your database. Following is a simple example −

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。

You are now logged into PostgreSQL testdb and ready to execute your commands inside testdb. To exit from the database, you can use the command \q.