在项目中使用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的配置,这样可以确保没有问题。

libpqxx接口的在linux下的使用,解决psql:connections on Unix domain socket "/tmp/.s.PGSQL.5432"错误的更多相关文章

  1. 由一个简单需求到Linux环境下的syslog、unix domain socket

    本文记录了因为一个简单的日志需求,继而对linux环境下syslog.rsyslog.unix domain socket的学习.本文关注使用层面,并不涉及rsyslog的实现原理,感兴趣的读者可以参 ...

  2. Linux下的IPC-UNIX Domain Socket【转】

    本文转载自:http://blog.csdn.net/guxch/article/details/7041052 一. 概述 UNIX Domain Socket是在socket架构上发展起来的用于同 ...

  3. linux一切皆文件之Unix domain socket描述符(二)

    一.知识准备 1.在linux中,一切皆为文件,所有不同种类的类型都被抽象成文件(比如:块设备,socket套接字,pipe队列) 2.操作这些不同的类型就像操作文件一样,比如增删改查等 3.主要用于 ...

  4. hadoop学习;大数据集在HDFS中存为单个文件;安装linux下eclipse出错解决;查看.class文件插件

    sudo apt-get install eclipse 安装后打开eclipse,提示出错 An error has occurred. See the log file /home/pengeor ...

  5. linux下开发,解决cocos2d-x中编译出现的一个小问题, undefined reference to symbol 'pthread_create@@GLIBC_2.2.5'

    解决cocos2d-x中编译出现的一个小问题 对于cocos2d-x 2.×中编译中,若头文件里引入了#include "cocos-ext.h",在进行C++编译的时候会遇到例如 ...

  6. linux下关于PCL(point cloud library)库的安装,三行命令错误的问题

    最近想再看看PCL,所以进行了安装,在之前的接触的过程中,由于之前的网络存在问题,导致以下三个命令: sudo add-apt-repository ppa:v-launchpad-jochen-sp ...

  7. 图形报表部署在Linux下出现乱码解决办法

     客户问题: 客户的操作系统SUSE LINUX Enterprise Server 10 (i586) 64位,服务器 weblogic8.1, JDK版本:jdk1.4.系统中只有图形报表展示 ...

  8. linux下log4j乱码解决

    使用log4j的时候,在WIN系统的时候正常显示中文,但是发布到linux系统的时候中文就显示成乱码了 由于log4j配置文件中没有设置编码格式(encoding),所以log4j就使用系统默认编码. ...

  9. linux下安装mysql解决乱码、时间差、表的大小写问题

    编辑vi /etc/mysql/my.cnf,有的则是:/etc/my.cnf,加入 [client]default-character-set=utf8mb4 [mysql]default-char ...

随机推荐

  1. java1 - 环境与简介

    一.阅读 JAVA历史 回答以下问题: JDK 是什么? JRE 是什么? java 有那三大平台? java 开发工具有那些? java 可以在那些系统上面做开发? java 工程师可以做什么? 二 ...

  2. qt 字符数组如何转换字符串?

    char 字符数组如何转换成 QString? char source{1024} = {0}; QString des = QString::fromLocal8Bit(source);

  3. 排序算法java实现

    1. 插入排序 原理:遍历到第N个元素的时候前面的N-1个元素已经是排序好的了,那么就查找前面的N-1个元素把这第N个元素放在合适的位置,如此下去直到遍历完序列的元素为止.    算法的复杂度也是简单 ...

  4. Linux下安装MySQL数据库(压缩包方式安装)

    1.这里我将Mysql安装在/usr/local/mysql目录里面,也可以安装在其他地方; mkdir /usr/local/mysql 2.下载MySQL压缩包 wget http://dev.M ...

  5. CPLD/FPGA厂商概述 .

    随着可编程逻辑器件应用的日益广泛,许多IC制造厂家涉足PLD/FPGA领域.目前世界上有十几家生产CPLD/FPGA的公司,最大的三家是:ALTERA,XILINX,Lattice,其中ALTERA和 ...

  6. linux下编译sphinx拓展

    编译libsphinxclient sphinx 源码包里的api文件夹下的libsphinxclient cd /root/api/libsphinxclient/ ./configure make ...

  7. Excel 2010高级应用-圆环图(七)

    Excel 2010高级应用-圆环图(七) 基本操作如下: 1.新建空白Excel文档,并命名为圆环图 2.单击"插入",并找到圆环图图样 3.单击圆环图图样,并在空白文档上生成图 ...

  8. AM335x(TQ335x)学习笔记——USB驱动移植

    对于AM335x来讲,TI维护的USB驱动已经非常完善了,本文称之为移植,实际上仅仅是配置内核选项使能USB HOST/OTG功能.废话少说,直接动手开启AM335x的USB驱动配置项. Step1. ...

  9. Caused by: java.net.SocketException: Software caused connection abort: socket write error

    1.错误描述 [ERROR:]2015-05-06 10:54:18,967 [异常拦截] ClientAbortException: java.net.SocketException: Softwa ...

  10. Exynos4412交叉编译环境搭建

    Exynos4412交叉编译环境搭建 交叉编译:在PC机(x86平台)上开发程序,在ARM板上运行,提高开发.编译速度. 环境: Tiny4412SDK1506开发板 需要软件: arm-linux- ...