前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住给大家分享一下。点击跳转到网站:https://www.captainai.net/dongkelun

前言
有客户用的数据库为 PolarDB-for-PostgreSQL 11 版本,为了验证 Flink CDC 是否适配,先在本地安装一个开源版本验证一下。
版本
- PolarDB-for-PostgreSQL 11.22
源码
克隆源码,然后切换对应的分支:1
git clone https://github.com/ApsaraDB/PolarDB-for-PostgreSQL.git
或者浏览器直接下载对应分支的zip包:
https://github.com/ApsaraDB/PolarDB-for-PostgreSQL/archive/refs/heads/REL_11_STABLE.zip
安装依赖
1 | sudo yum install -y git gcc gcc-c++ make readline readline-devel zlib-devel libxml2-devel libxslt-devel openssl-devel |
编译安装
1 | ./configure --prefix=/usr/local/polardb |
在编译安装时可能会遇到缺少依赖的情况,比如:1
2
3
4configure: error: readline library not found
If you have readline already installed, see config.log for details on the
failure. It is possible the compiler isn't looking in the proper directory.
Use --without-readline to disable readline support.
只需要安装对应的依赖即可:1
sudo yum install -y readline readline-devel
配置环境变量
1 | echo 'export PATH=/usr/local/polardb/bin:$PATH' >> /etc/profile |
验证版本:1
2postgres --version
postgres (PostgreSQL) 11.22
有些服务器可能已经安装过 PostgreSQL ,如果有这种情况,为了避免不必要的麻烦,后面使用相关的脚本命令可以使用绝对路径,比如 /usr/local/polardb/bin/postgres
初始化数据库
1 | initdb -D /var/lib/polardb/data -U postgres |
提示不能使用root用户
创建专用的数据库用户
1 | # 创建用户和用户组 |
创建数据目录并授权
1 | # 创建数据目录 |
再次初始化数据库
1 | su - polardb |
输出:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27The files belonging to this database system will be owned by user "polardb".
This user must also own the server process.
The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
Data page checksums are disabled.
fixing permissions on existing directory /var/lib/polardb/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Asia/Shanghai
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok
WARNING: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.
Success. You can now start the database server using:
pg_ctl -D /var/lib/polardb/data -l logfile start
启停数据库
1 | pg_ctl -D /var/lib/polardb/data start |
可以通过 -l 指定 日志文件:1
2
3
4pg_ctl -D /var/lib/polardb/data -l logfile start
waiting for server to start.... done
server started
这样启动日志就在日志文件 logfile 中:1
2
3
4
5
6
7cat logfile
2025-08-30 14:39:45.951 CST [3150899] LOG: listening on IPv6 address "::1", port 5432
2025-08-30 14:39:45.951 CST [3150899] LOG: listening on IPv4 address "127.0.0.1", port 5432
2025-08-30 14:39:45.951 CST [3150899] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2025-08-30 14:39:45.957 CST [3150900] LOG: database system was shut down at 2025-08-30 14:39:43 CST
2025-08-30 14:39:45.957 CST [3150899] LOG: database system is ready to accept connections
通过启动日志可以看出端口为 5432,且默认只允许本机访问,不允许远程连接,并且还有一个 /tmp/.s.PGSQL.5432 Unix 套接字文件,另外在 /tmp 目录下还有一个与之对应的锁文件:1
2
3ls /tmp -a | grep PGSQL
.s.PGSQL.5432
.s.PGSQL.5432.lock
以上我们也可以通过查看对应的端口进程得到:1
2
3
4
5netstat -nlp | grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 3150899/postgres
tcp6 0 0 ::1:5432 :::* LISTEN 3150899/postgres
unix 2 [ ACC ] STREAM LISTENING 1466749846 3150899/postgres /tmp/.s.PGSQL.5432
我们在启动时可以通过 -h 0.0.0.0 参数允许远程连接:
1 | # 先停止数据库 |
重启数据库命令:1
pg_ctl -D /var/lib/polardb/data restart
有的系统的Unix 域套接字和锁文件的存储目录默认不是/tmp,而是 /var/run/postgresql/ ,我们创建的 polardb 用户没有这个路径的权限,启动时会报错:1
FATAL: could not create lock file "/var/run/postgresql/.s.PGSQL.5432.lock": Permission denied
解决方法如下:
方法 1:指定自定义运行目录(推荐)
直接为 PolarDB 指定一个有写入权限的运行目录,避免使用系统默认的 /var/run/postgresql/:
1 | # 1. 切换到 polardb 用户(如果当前不是) |
参数说明:
- -h 0.0.0.0:允许远程连接
- -k /var/lib/polardb/run:指定 Unix 域套接字和锁文件的存储目录
方法 2:授权系统默认目录(需要 root 权限)
如果希望使用默认的 /var/run/postgresql/ 目录,可通过 root 用户授权:
1 | # 1. 切换到 root 用户 |
验证
端口号与进程
1 | netstat -nlp | grep 5432 |
验证版本
1 | # 1. postgres 命令直接查看 |
基础功能验证
数据库连接与操作测试
1 | -- 查看有哪些库 |
事务完整性测试
1 | -- 测试事务提交 |
权限与用户管理验证
1 | -- 创建新用户并授权 |
网络与连接验证
本地 socket 连接测试
如果启动时指定了 -k /var/lib/polardb/run(自定义 socket 目录),测试通过 socket 连接:
1 | # 使用指定的 socket 目录连接 |
验证点:无需 IP 地址,直接通过 socket 成功连接(排除 socket 权限或路径配置问题)。
远程 IP 连接测试
从另一台机器或本地用 IP 地址连接(需确保 -h 0.0.0.0 已配置,允许远程连接):1
2
3# 本地用 IP 连接(替换为实际 IP)
psql -U postgres -d testdb -h 192.168.1.1 # 你的服务器 IP
psql -U cdc_user -d testdb -h 192.168.1.1
默认情况下会报错:1
22025-08-30 15:20:53.300 CST [21383] FATAL: no pg_hba.conf entry for host "*.*.*.*", user "postgres", database "testdb"
psql: FATAL: no pg_hba.conf entry for host "*.*.*.*", user "postgres", database "testdb"
可以通过修改 /var/lib/polardb/data/pg_hba.conf ,添加:1
2
3
4# 允许 postgres 用户从所有 IP 访问所有数据库(开发环境临时用)
host all postgres 0.0.0.0/0 trust
# 允许 cdc_user 用户从所有 IP 访问所有数据库(开发环境临时用)
host all cdc_user 0.0.0.0/0 md5
修改后需让 PostgreSQL 重新加载配置:
1 | # 方式 1:重启数据库(会中断连接,生产需谨慎) |
其中 trust 表示允许无密码登录,因为默认情况下 postgres 没有设置密码,不设置 trust 就会报错(也可以给 postgres 用户设置密码):1
2
3
42025-08-30 15:28:23.926 CST [24848] DETAIL: User "postgres" has no password assigned.
Connection matched pg_hba.conf line 95: "host all postgres 0.0.0.0/0 md5"
psql: fe_sendauth: no password supplied
重新加载配置后验证: