notes/article/pg.md

99 lines
1.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 从源码安装 PostgreSQL
## 编译安装
解压缩后配置安装路径:
```bash
./configure --prefix=/usr/local/pg14
```
视安装路径是否需要高权限,执行 install:
```bash
make
sudo make install
```
## 用户设置
自己从源码安装,需要创建 postgres 用户:
```bash
sudo useradd -r -s /usr/sbin/nologin postgres
```
参数说明:
- -r 表示创建系统用户
- -s 指定登陆 shell使用 nologin 表示禁止用户登陆交互式 shell
使用 `createuser` 创建 `postgres` 超级用户。
```bash
$ createuser -s postgres -p 5532
```
删除用户:
```bash
$ dropuser postgres -p 5532
```
更新 /etc/passwd 文件,为 postgres 用户设置家目录 /data/pg。
## 启动实例
初始化数据库:
```bash
sudo -u postgres initdb /data/pg/master
```
启动服务:
```bash
sudo -u postgres pg_ctl -D master -l master.log start
```
从 16 版本起pg 编译需要 icu 支持,也可以忽略 icu
```bash
./configure --prefix=xxxx --without-icu
```
TODO: Debian 12 已经安装了 `libicu` 但是不起作用。
### pg17
在 Debian 12 中编译 pg17 需要安装 bison 和 flex
```bash
sudo apt install bison flex
```
## 安装插件
pg 源码的 contrib 目录中,有很多官方提供的插件,进入插件目录,用 make 进行编译安装。
```bash
$ make install
```
进入 pg 后,用 `create extension` 命令激活插件。
```sql
create extension pg_walinspect;
```
查看加载的插件列表:
```sql
select * from pg_extension;
```
```plaintext
oid extname extowner extnamespace extrelocatable extversion extconfig extcondition
----- ------------- -------- ------------ -------------- ---------- --------- ------------
14417 plpgsql 10 11 False 1.0 <null> <null>
16389 pg_walinspect 10 2200 True 1.1 <null> <null>
SELECT 2
```