为什么我无法从 Perl 连接到 postgres?
我相信我已经正确设置了 Pg,但我的脚本似乎没有连接到数据库。 我正在测试:
$database="networkem"; $user="postgres"; $password=""; $host="localhost"; $dbh = DBI->connect("DBI:Pg:dbname=$dbname;host=$host", $user, $password);
我的 pg_hba 读取:
host all postgres 127.0.0.1 255.255.255.255 trust
我可以通过命令行使用 psql ,并且已经使用 -i 选项启动了 postmaster 。 我缺少什么?
我还尝试与另一个通过 psql: 正常工作的用户一起
$user="jimbo"; $password="p2ssw0rd";
阅读 pg_hba:
host all jimbo 127.0.0.1 255.255.255.255 trust
I believe i have set up Pg properly, but my script doesn't seem to be connecting to the database. I am testing with:
$database="networkem"; $user="postgres"; $password=""; $host="localhost"; $dbh = DBI->connect("DBI:Pg:dbname=$dbname;host=$host", $user, $password);
My pg_hba reads:
host all postgres 127.0.0.1 255.255.255.255 trust
I can use psql
just fine via command-line and have started postmaster with -i option. What am I missing?
I also tried with another user that works fine via psql:
$user="jimbo"; $password="p2ssw0rd";
with pg_hba reading:
host all jimbo 127.0.0.1 255.255.255.255 trust
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
DBI->errstr
会说出连接失败的原因,而不是通过玩 20 个问题来调试您的设置。不过,如果我不得不猜测……由于 Postgres 根据主机和登录用户进行身份验证,我怀疑您提供给 Postgres 连接的用户名和您登录的 Unix 用户之间存在混淆。
Rather than play 20 questions to debug your setup,
DBI->errstr
will say why the connection failed.Though if I had to guess... since Postgres authenticates based on host and login user, I suspect the confusion lies between the user name you're giving to the Postgres connection and the Unix user you're logged in as.
除了 Schwern 的出色响应之外,您还可以检查 PostgreSQL 日志,根据 postgresql.conf 中选择的选项,该日志可能会告诉您很多有关错误的信息。
Besides Schwern's excellent response, you can also check PostgreSQL log which, depending on the options selected in postgresql.conf may tell you a lot about what was wrong.
建议您在 postgresql.conf 中使用“listen_addresses”配置选项,而不是命令行上的“-i”。 例如:
尝试以运行 Perl 脚本的同一用户身份执行以下命令:
'-h localhost' 强制使用网络连接而不是 Unix 套接字连接。 如果该命令有效,那么您的 perl 脚本也应该有效。
It is recommended that you use the 'listen_addresses' configuration option in your postgresql.conf instead of '-i' on the command line. For example:
Try executing the following command as the same user you are running your perl script with:
The '-h localhost' forces a network connection instead of a Unix socket connection. If that command works, your perl script should also work.
我遇到过同样的问题。 上面尝试“-h localhost”的提示证实我在通过网络连接时遇到问题。
将以下内容添加到 pg_hba.conf 修复了该问题。
I had the same issue. The hint above for trying "-h localhost" confirmed that I had a problem connecting over the network.
Adding the following to pg_hba.conf fixed the problem.
DBI 可能连接到 IPv4 或 IPv6 postgresql 服务器接口,具体取决于配置方式。
因此,您可能需要 pg_hba.conf 中的这两行:
IPv4:
IPv6:
DBI may be connecting to either the IPv4 or IPv6 postgresql server interface, depending on how things are configured.
So you may need both of these lines in pg_hba.conf:
IPv4:
IPv6: