PostgreSQL - 選擇資料庫



本章介紹了訪問資料庫的各種方法。假設我們已經在上一章中建立了一個數據庫。您可以使用以下任何一種方法選擇資料庫:

  • 資料庫 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。

廣告

© . All rights reserved.