前言
有客户用的数据库为 PolarDB-for-PostgreSQL 11 版本,为了验证 Flink CDC 是否适配,先在本地安装一个开源版本验证一下。
版本
- PolarDB-for-PostgreSQL 11.22
源码
克隆源码,然后切换对应的分支:
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
安装依赖
sudo yum install -y git gcc gcc-c++ make readline readline-devel zlib-devel libxml2-devel libxslt-devel openssl-devel
编译安装
./configure --prefix=/usr/local/polardb
make -j 4
sudo make install
在编译安装时可能会遇到缺少依赖的情况,比如:
configure: 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.
只需要安装对应的依赖即可:
sudo yum install -y readline readline-devel
配置环境变量
echo 'export PATH=/usr/local/polardb/bin:$PATH' >> /etc/profile
source /etc/profile
验证版本:
postgres --version
postgres (PostgreSQL) 11.22
有些服务器可能已经安装过 PostgreSQL ,如果有这种情况,为了避免不必要的麻烦,后面使用相关的脚本命令可以使用绝对路径,比如 /usr/local/polardb/bin/postgres
初始化数据库
initdb -D /var/lib/polardb/data -U postgres
initdb: cannot be run as root
Please log in (using, e.g., "su") as the (unprivileged) user that will
own the server process.
提示不能使用root用户
创建专用的数据库用户
# 创建用户和用户组
sudo groupadd polardb
sudo useradd -g polardb polardb
# 设置用户密码(可选,按提示输入即可)
sudo passwd polardb
创建数据目录并授权
# 创建数据目录
sudo mkdir -p /var/lib/polardb/data
# 授权目录给 polardb 用户
sudo chown -R polardb:polardb /var/lib/polardb
再次初始化数据库
su - polardb
initdb -D /var/lib/polardb/data -U postgres
输出:
The 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
启停数据库
pg_ctl -D /var/lib/polardb/data start
waiting for server to start....2025-08-30 14:30:13.396 CST [3150725] LOG: listening on IPv6 address "::1", port 5432
2025-08-30 14:30:13.396 CST [3150725] LOG: listening on IPv4 address "127.0.0.1", port 5432
2025-08-30 14:30:13.396 CST [3150725] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2025-08-30 14:30:13.402 CST [3150726] LOG: database system was shut down at 2025-08-30 14:30:05 CST
2025-08-30 14:30:13.403 CST [3150725] LOG: database system is ready to accept connections
done
server started
可以通过 -l 指定 日志文件:
pg_ctl -D /var/lib/polardb/data -l logfile start
waiting for server to start.... done
server started
这样启动日志就在日志文件 logfile 中:
cat 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 目录下还有一个与之对应的锁文件:
ls /tmp -a | grep PGSQL
.s.PGSQL.5432
.s.PGSQL.5432.lock
以上我们也可以通过查看对应的端口进程得到:
netstat -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 参数允许远程连接:
# 先停止数据库
pg_ctl -D /var/lib/polardb/data stop
# 启动数据库允许远程连接
pg_ctl -D /var/lib/polardb/data -o "-h 0.0.0.0" start
waiting for server to start....2025-08-30 14:53:17.857 CST [3151214] LOG: listening on IPv4 address "0.0.0.0", port 5432
2025-08-30 14:53:17.858 CST [3151214] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2025-08-30 14:53:17.864 CST [3151215] LOG: database system was shut down at 2025-08-30 14:52:17 CST
2025-08-30 14:53:17.865 CST [3151214] LOG: database system is ready to accept connections
done
server started
netstat -nlp | grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 3151214/postgres
unix 2 [ ACC ] STREAM LISTENING 1466715811 3151214/postgres /tmp/.s.PGSQL.5432
重启数据库命令:
pg_ctl -D /var/lib/polardb/data restart
有的系统的Unix 域套接字和锁文件的存储目录默认不是/tmp,而是 /var/run/postgresql/ ,我们创建的 polardb 用户没有这个路径的权限,启动时会报错:
FATAL: could not create lock file "/var/run/postgresql/.s.PGSQL.5432.lock": Permission denied
解决方法如下:
方法 1:指定自定义运行目录(推荐)
直接为 PolarDB 指定一个有写入权限的运行目录,避免使用系统默认的 /var/run/postgresql/:
# 1. 切换到 polardb 用户(如果当前不是)
su - polardb
# 2. 创建自定义运行目录
mkdir -p /var/lib/polardb/run
# 3. 启动数据库时指定运行目录
pg_ctl -D /var/lib/polardb/data -o "-h 0.0.0.0 -k /var/lib/polardb/run" start
参数说明:
- -h 0.0.0.0:允许远程连接
- -k /var/lib/polardb/run:指定 Unix 域套接字和锁文件的存储目录
方法 2:授权系统默认目录(需要 root 权限)
如果希望使用默认的 /var/run/postgresql/ 目录,可通过 root 用户授权:
# 1. 切换到 root 用户
exit # 退出 polardb 用户,回到 root
# 2. 创建目录并授权
mkdir -p /var/run/postgresql/
chown -R polardb:polardb /var/run/postgresql/
# 3. 切换回 polardb 用户启动
su - polardb
pg_ctl -D /var/lib/polardb/data start
验证
端口号与进程
netstat -nlp | grep 5432
ps aux | grep postgres
验证版本
# 1. postgres 命令直接查看
postgres --version
postgres (PostgreSQL) 11.22
# 2.连接数据库后,通过 SELECT version(); 验证
psql -U postgres -d postgres -h 127.0.0.1
psql (11.22)
Type "help" for help.
postgres=# SELECT version();
version
-------------------------------------------------------------------------------
PostgreSQL 11.22 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 10.3.1, 64-bit
(1 row)
基础功能验证
数据库连接与操作测试
-- 查看有哪些库
\l
-- 连接数据库后,创建测试库和表
CREATE DATABASE testdb;
-- 切换到测试库
\c testdb;
-- 创建测试表
CREATE TABLE test_cdc (
id INT PRIMARY KEY,
name VARCHAR(50),
create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- 插入数据
INSERT INTO test_cdc (id, name) VALUES (1, 'polar_test');
INSERT INTO test_cdc (id, name) VALUES (2, 'flink_cdc_test');
-- 查询数据
SELECT * FROM test_cdc;
-- 更新数据
UPDATE test_cdc SET name = 'updated' WHERE id = 1;
-- 删除数据
DELETE FROM test_cdc WHERE id = 2;
事务完整性测试
-- 测试事务提交
BEGIN;
INSERT INTO test_cdc (id, name) VALUES (3, 'transaction_test');
COMMIT;
SELECT * FROM test_cdc WHERE id = 3;
-- 应能查询到
-- 测试事务回滚
BEGIN;
INSERT INTO test_cdc (id, name) VALUES (4, 'rollback_test');
ROLLBACK;
SELECT * FROM test_cdc WHERE id = 4;
-- 应查询不到
权限与用户管理验证
-- 创建新用户并授权
CREATE USER cdc_user WITH PASSWORD 'cdc_password';
GRANT CONNECT ON DATABASE testdb TO cdc_user;
GRANT SELECT, INSERT, UPDATE, DELETE ON test_cdc TO cdc_user;
-- 退出当前连接,用新用户登录
\q
psql -U cdc_user -d testdb -h 127.0.0.1
-- 测试权限(应能执行授权操作)
INSERT INTO test_cdc (id, name) VALUES (5, 'user_test');
SELECT * FROM test_cdc;
-- 测试越权操作(应报错)
DROP TABLE test_cdc;
2025-08-30 15:15:43.913 CST [3152301] ERROR: must be owner of table test_cdc
2025-08-30 15:15:43.913 CST [3152301] STATEMENT: DROP TABLE test_cdc;
ERROR: must be owner of table test_cdc
网络与连接验证
本地 socket 连接测试
如果启动时指定了 -k /var/lib/polardb/run(自定义 socket 目录),测试通过 socket 连接:
# 使用指定的 socket 目录连接
psql -U postgres -d testdb -h /var/lib/polardb/run
验证点:无需 IP 地址,直接通过 socket 成功连接(排除 socket 权限或路径配置问题)。
远程 IP 连接测试
从另一台机器或本地用 IP 地址连接(需确保 -h 0.0.0.0 已配置,允许远程连接):
# 本地用 IP 连接(替换为实际 IP)
psql -U postgres -d testdb -h 192.168.1.1 # 你的服务器 IP
psql -U cdc_user -d testdb -h 192.168.1.1
默认情况下会报错:
2025-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 ,添加:
# 允许 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:重启数据库(会中断连接,生产需谨慎)
pg_ctl -D /var/lib/polardb/data restart
# 方式 2:平滑重载配置(推荐)
pg_ctl -D /var/lib/polardb/data reload
其中 trust 表示允许无密码登录,因为默认情况下 postgres 没有设置密码,不设置 trust 就会报错(也可以给 postgres 用户设置密码):
2025-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
重新加载配置后验证: