我正在使用逻辑复制。我订阅了如下订阅。
=# CREATE SUBSCRIPTION mysub CONNECTION 'host=xxx.xxx.xxx.xxx port=5432
user=postgres dbname=mydb password=<password>' PUBLICATION mypub;
NOTICE: created replication slot "mysub" on publisher
CREATE SUBSCRIPTION
但我想知道我是否可以使用.pgpass文件来提供密码。 当然,我试过了。但它失败如下。
=# CREATE SUBSCRIPTION mysub CONNECTION 'host=xxx.xxx.xxx.xxx port=5432
user=postgres dbname=mydb' PUBLICATION mypub;
ERROR: could not connect to the publisher: fe_sendauth: no password supplied
[我的.pgpass]
localhost:5432:postgres:postgres:<password>
localhost:5432:mydb:postgres:<password>
xxx.xxx.xxx.xxx:5432:mydb:postgres:<password>
这个.pgpass文件适用于pgAgent。
我可以使用.pgpass文件进行逻辑复制吗?或者我应该在CREATE语句中写下我的密码吗?如果在CREATE命令中写密码是唯一的答案,它是否安全?
答案 0 :(得分:2)
https://www.postgresql.org/docs/10/static/sql-createsubscription.html
CONNECTION'conninfo'发布者的连接字符串。对于 详情见
https://www.postgresql.org/docs/10/static/libpq-connect.html#LIBPQ-CONNSTRING
passfile
指定用于存储密码的文件的名称(请参阅 第33.15节)。默认为〜/ .pgpass
是的 - 它应该有效。让我们模拟一下。首先,我故意使用错误的passfile来查看它是否反映在错误中:
t=# CREATE SUBSCRIPTION mysub CONNECTION 'host=localhost port=5433 passfile=/tmp/p user=vao dbname=t' PUBLICATION mypub;
ERROR: could not connect to the publisher: fe_sendauth: no password supplied
不,不是,但检查日志会:
-bash-4.2$ tail /pg/d10/log/postgresql-Tue.log | grep WARN | tail -n 1
WARNING: password file "/tmp/p" has group or world access; permissions should be u=rw (0600) or less
好的,请尝试使用默认值:
t=# CREATE SUBSCRIPTION mysub CONNECTION 'host=localhost port=5433 user=vao dbname=t' PUBLICATION mypub;
ERROR: could not connect to the publisher: fe_sendauth: no password supplied
这次甚至没有警告!所以检查chmod:
-bash-4.2$ ll ~/.pgpass
-r-------- 1 postgres postgres 1227 May 15 15:00 /home/vao/.pgpass
看起来不错,但是啊哈 - 这个连接没有线路,因为下面要求输入密码:
-bash-4.2$ psql -h localhost -p 5433 -U vao t
Password for user vao:
这样:
echo '*:*:*:vao:blah' > ~/.pgpass
-bash-4.2$ psql -h localhost -p 5433 -U vao t
psql: FATAL: password authentication failed for user "vao"
password retrieved from file "/var/lib/pgsql93/.pgpass"
啊哈 - 现在它使用它,所以回到SQL:
t=# CREATE SUBSCRIPTION mysub CONNECTION 'host=localhost port=5433 user=vao dbname=t' PUBLICATION mypub;
ERROR: could not connect to the publisher: FATAL: password authentication failed for user "vao"
password retrieved from file "/var/lib/pgsql93/.pgpass"
是的,您可以使用指定的和默认的pgpassword文件进行逻辑复制订阅
答案 1 :(得分:1)
[已解决]我确实喜欢这个。
在.pgpass
(IP of publisher):5432:mydb:postgres:(my password)
将.pgpass的所有者,群组更改为“postgres”
-rw-------. 1 postgres postgres 163 5월 18 06:06 .pgpass
(.pgpass的所有者和群组是'pgagent',“fe_sendauth:没有提供密码”。)
登录DB
后=# create subscription mysub connection 'host=(IP of publisher) port=5432 user=postgres dbname=mydb passfile=/var/lib/pgsql/.pgpass' publication mypub;
效果很好^^