---
title: 0821 - 安裝 PostgreSQL 11 / 12 (Linux / windows WSL)
tags: PostgreSQL, Linux
---
# 安裝 PostgreSQL 11 / 12 (Linux)
## 測試環境 (LAN)
| | Host IP | OS | Core | Ram | Disk |
| --------- | -------------- | ------------------- | ---- | ----- | ------ |
| Client | 192.168.30.1 | maxOS 10.15.5 | 4 | 16 GB | 256 GB |
| Server 01 | 192.168.30.149 | Ubuntu 18.04 server | 2 | 4 GB | 20 GB |
| Server 02 | 192.168.30.151 | Centos 7 2003 | 2 | 4 GB | 20 GB |
## 參考資料
- [這兒](https://computingforgeeks.com/install-postgresql-11-on-ubuntu-linux/)
## Centos 7
- [官方安裝方法](https://www.postgresql.org/download/linux/redhat/)
## 方法一
### 安裝 Ubuntu 套件庫的 PostgreSQL
```
# 更新套件庫
sudo apt update -y
```
### 安裝 PostgreSQL 相關套件
```
# 安裝 PostgreSQL
sudo apt install postgresql postgresql-contrib
```
### 安裝完會自動啟動 PostgreSQL 服務,查看服務是否正常啟動
```
# 查看 PostgreSQL 服務狀態
sudo systemctl status postgresql
```
看到 `Active: active` 表示已經正常啟動了
```
root@ubuntu:~# systemctl status postgresql
● postgresql.service - PostgreSQL RDBMS
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled)
Active: active (exited) since Fri 2020-08-21 13:42:23 UTC; 39s ago
Main PID: 3335 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 4632)
CGroup: /system.slice/postgresql.service
Aug 21 13:42:23 ubuntu systemd[1]: Starting PostgreSQL RDBMS...
Aug 21 13:42:23 ubuntu systemd[1]: Started PostgreSQL RDBMS.
```
### 設定開機時自動啟動服務
```
sudo systemctl enable postgresql
```
## 方法二
### 安裝 PostgreSQL 官方套件庫
[PostgreSQL Doc](https://www.postgresql.org/download/linux/ubuntu/)
```
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
# 如果要安裝其它版本,可以把下方 postgresql 改成 postgresql-11 或其它指定版號
sudo apt-get -y install postgresql
```
```
# 設定開機啟動
sudo systemctl enable postgresql
```
### PostgreSQL 官方套件庫的其它相關套件
| 套件名稱 | 說明 |
| ------------------------ | --------------------------------------------------------------------------------------- |
| postgresql-client-12 | client libraries and client binaries |
| postgresql-12 | core database server |
| postgresql-contrib-9.x | additional supplied modules (part of the postgresql-xx package in version 10 and later) |
| libpq-dev | libraries and headers for C language frontend development |
| postgresql-server-dev-12 | libraries and headers for C language backend development |
| pgadmin4 | pgAdmin 4 graphical administration utility |
```
# 安裝其它套件可以使用 apt install + 套件名
sudo apt install pgadmin4
```
### 設定非本機連接 PostgreSQL
- 編輯 `postgresql.conf` 檔案
- 用 ==方法二== 安裝的設定檔案路徑在 `/etc/postgresql/12/main/postgresql.conf`
- windows 的 `WSL` 沒有 `systemctl` 的指令可以使用,請安裝 winodws 版的 `PostgreSQL`
```
...略
# - Connection Settings -
listen_addresses = 'localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
#superuser_reserved_connections = 3 # (change requires restart)
unix_socket_directories = '/var/run/postgresql' # comma-separated list of directories
# (change requires restart)
...略
```
- 將 `listen_addresses = 'localhost'` 改成 server 對外的 ip 位址
- 測試環境的 server ip 位址是 `192.168.30.149` ,設定如下圖

- 接著要修改 `/etc/postgresql/12/main/pg_hba.conf`
- 新增一行 ``host all all 192.168.30.1/32 trust``
```
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# 新增下面這一行 #
host all all 192.168.30.1/32 trust
```
- 設定完成後,重新啟動 PostgreSQL 服務
```
sudo systemctl restart postgresql
```
- 切換至 `postgres` 使用者並進入 `postgresql shell` 更改密碼
```
sudo -u postgres psql
```
- 變更密碼 ==方法一==
```
# 這方法會留下指令的歷史記錄,其它管理者進來會看到設定的密碼,較不推薦
postgres-# ALTER USER postgres PASSWORD 'your_new_password';
```
- 變更密碼 ==方法二== (recommend)
```
# 使用 \password 變更
postgres-# \password
# 輸入時密碼不會顯示
Enter new password:
Enter it again:
postgres-#
# 離開 postgresql shell
postgres-# \q
```
- 接著再修改 `pg_hba.conf`,把剛剛新增 `host all all 192.168.30.1/32 trust` 中的 ==trust== 改成 ==md5==
- 再重新啟動 PostgreSQL 服務
```
sudo systemctl restart postgresql
```
### `psql` 指令相關
- 以 postgres 身份進入 PostgreSQL shell
本機環境可以省略 `-h localhost`
```
sudo psql -U postgres -h localhost
```
- General options
```
General options:
-c, --command=COMMAND run only single command (SQL or internal) and exit
-d, --dbname=DBNAME database name to connect to (default: "root")
-f, --file=FILENAME execute commands from file, then exit
-l, --list list available databases, then exit
-v, --set=, --variable=NAME=VALUE
set psql variable NAME to VALUE
(e.g., -v ON_ERROR_STOP=1)
-V, --version output version information, then exit
-X, --no-psqlrc do not read startup file (~/.psqlrc)
-1 ("one"), --single-transaction
execute as a single transaction (if non-interactive)
-?, --help[=options] show this help, then exit
--help=commands list backslash commands, then exit
--help=variables list special variables, then exit
```
- Connection options (連接資料庫相關參數)
```
Connection options:
-h, --host=HOSTNAME database server host or socket directory (default: "local socket")
-p, --port=PORT database server port (default: "5432")
-U, --username=USERNAME database user name (default: "root")
-w, --no-password never prompt for password
-W, --password force password prompt (should happen automatically)
```
# 安裝 PostgreSQL (windows WSL)
## 請參考微軟官方 WSL 安裝 Postgresql 的方式
- [微軟官方安裝說明](https://docs.microsoft.com/zh-tw/windows/wsl/tutorials/wsl-database)
# 系統管理
## 服務配置與維運
待更新
## 服務組態設定
待更新
## 使用者認證
待更新
## 資料庫角色
待更新
## 例行性資料庫維護工作
待更新
## 高可用性 (HA)、負載平衡 (Load Balancing) 與副本 (Replication)
待更新
## 監控資料庫活動
待更新
## 監控磁碟使用情況
待更新