Perl - 连接到 PostgreSQL 数据库 - 未能找到正确的套接字

问题描述 投票:0回答:1

我可以与 PHP 良好连接,并在本地使用 Psql 连接,但 Perl 不能。

my $dbh = DBI->connect("DBI:Pg:dbname=mydb,host=localhost:5432","user","pass",{'RaiseError' => 1});

我相信错误是因为我的套接字位于 tmp:

postgres@host/opt/psql/bin $ netstat -an | grep 5432
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN
tcp6       0      0 :::5432                 :::*                    LISTEN
unix  2      [ ACC ]     STREAM     LISTENING     24728255 /tmp/.s.PGSQL.5432
unix  3      [ ]         STREAM     CONNECTED     24729004 /tmp/.s.PGSQL.5432

当我运行简单的 Perl 脚本时,它似乎在 /var/run 中查找:

./test.pl
DBI connect('dbname=mydb','user',...) failed: could not connect to server: No such file or directory
    Is the server running locally and accepting
    connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"? at ./test.pl line 6

我尝试简单地创建一个符号链接,但这似乎不起作用:

sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
ln: failed to create symbolic link `/var/run/postgresql/.s.PGSQL.5432': No such file or directory

其他一些简单的东西: pg_hba.conf 信任所有本地主机连接以及我的子网的连接。

postgresql.conf 有以下内容:
监听地址 = '*'

postgresql perl dbi
1个回答
3
投票

参见 http://www.postgresql.org/docs/9.2/static/libpq-connect.html#LIBPQ-CONNECT-HOST

主持人

要连接的主机的名称。如果以斜杠开头,则指定 Unix域通信而不是TCP/IP通信;价值 是存储套接字文件的目录的名称。这 未指定主机时的默认行为是连接到 /tmp 中的 Unix 域套接字(或指定的任何套接字目录) 当 PostgreSQL 构建时)。在没有 Unix 域套接字的机器上, 默认是连接到本地主机。

因此,具有

host=/tmp
的连接字符串应该使您的 Pg 客户端看起来在正确的位置(无论如何,这应该是默认位置)。

© www.soinside.com 2019 - 2024. All rights reserved.