为什么我无法从 Perl 连接到 postgres?

发布于 2024-07-10 06:23:13 字数 548 浏览 9 评论 0原文

我相信我已经正确设置了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

乱了心跳 2024-07-17 06:23:13

DBI->errstr 会说出连接失败的原因,而不是通过玩 20 个问题来调试您的设置。

my $dbh = DBI->connect(...) or die DBI->errstr;

不过,如果我不得不猜测……由于 Postgres 根据主机和登录用户进行身份验证,我怀疑您提供给 Postgres 连接的用户名和您登录的 Unix 用户之间存在混淆。

Rather than play 20 questions to debug your setup, DBI->errstr will say why the connection failed.

my $dbh = DBI->connect(...) or die DBI->errstr;

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.

望笑 2024-07-17 06:23:13

除了 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.

混吃等死 2024-07-17 06:23:13

建议您在 postgresql.conf 中使用“listen_addresses”配置选项,而不是命令行上的“-i”。 例如:

listen_addresses = '*'

尝试以运行 Perl 脚本的同一用户身份执行以下命令:

psql -U postgres -h localhost networkem

'-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:

listen_addresses = '*'

Try executing the following command as the same user you are running your perl script with:

psql -U postgres -h localhost networkem

The '-h localhost' forces a network connection instead of a Unix socket connection. If that command works, your perl script should also work.

粉红×色少女 2024-07-17 06:23:13

我遇到过同样的问题。 上面尝试“-h localhost”的提示证实我在通过网络连接时遇到问题。

将以下内容添加到 pg_hba.conf 修复了该问题。

host all postgres 127.0.0.1/32 trust

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.

host all postgres 127.0.0.1/32 trust
甜尕妞 2024-07-17 06:23:13

DBI 可能连接到 IPv4 或 IPv6 postgresql 服务器接口,具体取决于配置方式。

因此,您可能需要 pg_hba.conf 中的这两行:

IPv4:

host  all  <user>  127.0.0.1/32  trust

IPv6:

host  all  <user>  ::1/128       trust

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:

host  all  <user>  127.0.0.1/32  trust

IPv6:

host  all  <user>  ::1/128       trust
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文