- PostgreSQL 教程
- PostgreSQL - 首頁
- PostgreSQL - 概述
- PostgreSQL - 環境設定
- PostgreSQL - 語法
- PostgreSQL - 資料型別
- PostgreSQL - 建立資料庫
- PostgreSQL - 選擇資料庫
- PostgreSQL - 刪除資料庫
- PostgreSQL - 建立表
- PostgreSQL - 刪除表
- PostgreSQL - 模式
- PostgreSQL - 插入查詢
- PostgreSQL - 選擇查詢
- PostgreSQL - 運算子
- PostgreSQL - 表示式
- PostgreSQL - WHERE 子句
- PostgreSQL - AND & OR 子句
- PostgreSQL - 更新查詢
- PostgreSQL - 刪除查詢
- PostgreSQL - LIKE 子句
- PostgreSQL - LIMIT 子句
- PostgreSQL - ORDER BY 子句
- PostgreSQL - GROUP BY
- PostgreSQL - WITH 子句
- PostgreSQL - HAVING 子句
- PostgreSQL - DISTINCT 關鍵字
- 高階 PostgreSQL
- PostgreSQL - 約束
- PostgreSQL - 連線
- PostgreSQL - UNION 子句
- PostgreSQL - NULL 值
- PostgreSQL - 別名語法
- PostgreSQL - 觸發器
- PostgreSQL - 索引
- PostgreSQL - ALTER TABLE 命令
- TRUNCATE TABLE 命令
- PostgreSQL - 檢視
- PostgreSQL - 事務
- PostgreSQL - 鎖
- PostgreSQL - 子查詢
- PostgreSQL - 自動遞增
- PostgreSQL - 許可權
- 日期/時間函式和運算子
- PostgreSQL - 函式
- PostgreSQL - 常用函式
- PostgreSQL 介面
- PostgreSQL - C/C++
- PostgreSQL - Java
- PostgreSQL - PHP
- PostgreSQL - Perl
- PostgreSQL - Python
- PostgreSQL 有用資源
- PostgreSQL - 快速指南
- PostgreSQL - 有用資源
- PostgreSQL - 討論
PostgreSQL - 建立資料庫
本章討論如何在 PostgreSQL 中建立一個新的資料庫。PostgreSQL 提供兩種建立新資料庫的方法:
- 使用 CREATE DATABASE,這是一個 SQL 命令。
- 使用createdb,這是一個命令列可執行檔案。
使用 CREATE DATABASE
此命令將從 PostgreSQL shell 提示符建立資料庫,但您應該具有建立資料庫的相應許可權。預設情況下,新資料庫將透過克隆標準系統資料庫template1來建立。
語法
CREATE DATABASE 語句的基本語法如下:
CREATE DATABASE dbname;
其中dbname是要建立的資料庫的名稱。
示例
以下是一個簡單的示例,它將在您的 PostgreSQL 模式中建立testdb
postgres=# CREATE DATABASE testdb; postgres-#
使用 createdb 命令
PostgreSQL 命令列可執行檔案createdb是 SQL 命令CREATE DATABASE的包裝器。此命令與 SQL 命令CREATE DATABASE之間的唯一區別在於,前者可以直接從命令列執行,並且它允許在一個命令中將註釋新增到資料庫中。
語法
createdb的語法如下所示:
createdb [option...] [dbname [description]]
引數
下表列出了引數及其說明。
| 序號 | 引數和說明 |
|---|---|
| 1 | dbname 要建立的資料庫的名稱。 |
| 2 | description 指定要與新建立的資料庫關聯的註釋。 |
| 3 | options createdb 接受的命令列引數。 |
選項
下表列出了 createdb 接受的命令列引數:
| 序號 | 選項和說明 |
|---|---|
| 1 | -D tablespace 指定資料庫的預設表空間。 |
| 2 | -e 回顯 createdb 生成併發送到伺服器的命令。 |
| 3 | -E encoding 指定此資料庫中使用的字元編碼方案。 |
| 4 | -l locale 指定此資料庫中使用的區域設定。 |
| 5 | -T template 指定從中構建此資料庫的模板資料庫。 |
| 6 | --help 顯示有關 createdb 命令列引數的幫助資訊,然後退出。 |
| 7 | -h host 指定伺服器正在執行的機器的主機名。 |
| 8 | -p port 指定伺服器正在偵聽連線的 TCP 埠或本地 Unix 域套接字副檔名。 |
| 9 | -U username 連線到的使用者名稱。 |
| 10 | -w 從不發出密碼提示。 |
| 11 | -W 強制 createdb 在連線到資料庫之前提示輸入密碼。 |
開啟命令提示符並轉到安裝 PostgreSQL 的目錄。轉到 bin 目錄並執行以下命令以建立資料庫。
createdb -h localhost -p 5432 -U postgres testdb password ******
上面給出的命令將提示您輸入 PostgreSQL 管理員使用者的密碼,預設情況下為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-#