zoukankan      html  css  js  c++  java
  • libpqxx接口的在linux下的使用,解决psql:connections on Unix domain socket "/tmp/.s.PGSQL.5432"错误

    在项目中使用postgresql数据库时要求在windows和linux双平台兼容。于是在windows下使用的接口在linux下爆出异常:

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

    经过分析和查看文档解决了这个问题:

    1.原因:这是由于在linux下连接pg时使用了默认的unix domain socket方式连接pg时出现的,并且pg的端口运行在非默认端口上(默认端口是5432)。在windows下同样的接口使用了socket的TCP协议连接pg的。

    2.在使用pgxx::connection("")时,要传入的字符串不完整,导致了在linux下使用了unix domain socket方式连接。

    以下是postgresql官方文档解释:

    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.

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

    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.

    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:

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

    • 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的配置,这样可以确保没有问题。

  • 相关阅读:
    大伯的八十岁寿辰
    收到 wincore.cpp 中一个 " ASSERT " BUG: 当 MFC 应用程序 Visual C++ 中 MFC 规则 DLL 中调用函数声明
    命名空间Microsoft.Office.Interop.Word不存在....
    中国测绘科学研究院招聘WEBGIS开发工程师与客座研究生
    开源GIS学习笔记 sinoly BLOGJAVA
    2007年7月9日星期一
    转载:使用感受: VC2005 VC6.0
    推荐:用ogr和PIL把矢量数据转化成栅格图像
    《赢在中国》感言
    VS2005的断点无效问题
  • 原文地址:https://www.cnblogs.com/zhangdewang/p/8600242.html
Copyright © 2011-2022 走看看