libpqxx接口的在linux下的使用,解決psql:connections on Unix domain socket "/tmp/.s.PGSQL.5432"錯誤

在項目中使用postgresql數據庫時要求在windows和linux雙平臺兼容。因而在windows下使用的接口在linux下爆出異常:html

psql:connections on Unix domain socket "/tmp/.s.PGSQL.5432"

通過分析和查看文檔解決了這個問題:linux

1.緣由:這是因爲在linux下鏈接pg時使用了默認的unix domain socket方式鏈接pg時出現的,而且pg的端口運行在非默認端口上(默認端口是5432)。在windows下一樣的接口使用了socket的TCP協議鏈接pg的。sql

2.在使用pgxx::connection("")時,要傳入的字符串不完整,致使了在linux下使用了unix domain socket方式鏈接。數據庫

如下是postgresql官方文檔解釋:windows

host

Name of host to connect to. If a host name begins with a slash, it specifies Unix-domain communication rather than TCP/IP communication; the value is the name of the directory in which the socket file is stored. If multiple host names are specified, each will be tried in turn in the order given. The default behavior when host is not specified is to connect to a Unix-domain socket in /tmp (or whatever socket directory was specified when PostgreSQL was built). On machines without Unix-domain sockets, the default is to connect tolocalhost.app

A comma-separated list of host names is also accepted, in which case each host name in the list is tried in order. less

hostaddr

Numeric IP address of host to connect to. This should be in the standard IPv4 address format, e.g., 172.28.40.9. If your machine supports IPv6, you can also use those addresses. TCP/IP communication is always used when a nonempty string is specified for this parameter.dom

Using hostaddr instead of host allows the application to avoid a host name look-up, which might be important in applications with time constraints. However, a host name is required for GSSAPI or SSPI authentication methods, as well as for verify-full SSL certificate verification. The following rules are used:socket

  • If host is specified without hostaddr, a host name lookup occurs.ide

  • If hostaddr is specified without host, the value for hostaddr gives the server network address. The connection attempt will fail if the authentication method requires a host name.

  • If both host and hostaddr are specified, the value for hostaddr gives the server network address. The value for host is ignored unless the authentication method requires it, in which case it will be used as the host name.

Note that authentication is likely to fail if host is not the name of the server at network address hostaddr. Also, note that host rather than hostaddr is used to identify the connection in a password file .

A comma-separated list of hostaddr values is also accepted, in which case each host in the list is tried in order. See Section 33.1.1.3 for details.

Without either a host name or host address, libpq will connect using a local Unix-domain socket; or on machines without Unix-domain sockets, it will attempt to connect to localhost.

所以,咱們的接口主要發生了2件錯誤:

1.std::string strConnection在windows下使用strConnection = "dbname="+pg_dbname+" user="+pg_username+" password="時,string的拼接出現問題。因此再拼接string字符串時使用

std::string strConnection = "dbname="; strConnection += pg_dbname.c_str(); strConnection += " user="; strConnection += pg_username.c_str(); strConnection += " password="; strConnection += pg_password.c_str(); strConnection += " hostaddr="; strConnection += pg_ip.c_str(); strConnection += " port="; strConnection += pg_port.c_str();

2.在strConnection字符串中最好加上host=localhost的配置,這樣能夠確保沒有問題。

相關文章
相關標籤/搜索