Can';t将postgreSQL与psycopg2连接

Can';t将postgreSQL与psycopg2连接,postgresql,psycopg2,Postgresql,Psycopg2,这是我第一次找不到一些技术问题的答案 以下是我的问题: >> conn=psycopg2.connect(database="mydb", user="postgres", password="123",port=5432) Traceback (most recent call last): File "<stdin>", line 1, in <module> psycopg2.OperationalError: could not connect

这是我第一次找不到一些技术问题的答案 以下是我的问题:

>> conn=psycopg2.connect(database="mydb", user="postgres", password="123",port=5432)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
psycopg2.OperationalError: 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"?
连接(database=“mydb”,user=“postgres”,password=“123”,port=5432) 回溯(最近一次呼叫最后一次): 文件“”,第1行,在 psycopg2.OperationalError:无法连接到服务器:没有此类文件或目录 服务器是否在本地运行并接受 Unix域套接字上的连接“/var/run/postgresql/.s.PGSQL.5432”?
  • 我的postgreSQL正在运行
  • 我的ListengPort肯定是5432
  • root@lanston-笔记本电脑:~#psql-l 密码:
  • 数据库清单 名称|所有者|编码|排序规则| Ctype |访问权限 ---------------+----------+----------+------------+------------+----------------------- 检查数据库| postgres | UTF8 | en|u US.UTF8 | en|u US.UTF8| mydb | postgres | UTF8 | en|u US.UTF8 | en|u US.UTF8| 博士后|博士后| UTF8 | en|U US.UTF8 | en|U US.UTF8| 模板0 | postgres | UTF8 | en|u US.UTF8 | en|u US.UTF8 |=c/postgres+ || | | | postgres=CTc/postgres 模板1 | postgres | UTF8 | en|u US.UTF8 | en|u US.UTF8 |=c/postgres+ || | | | postgres=CTc/postgres
    非常感谢

    psycopg2使用的libpq期望Postgres套接字位于
    /var/run/postgresql/
    中,但当您从源代码安装Postgres时,默认情况下它位于
    /tmp/

    检查是否存在文件
    /tmp/.s.PGSQL.5432
    ,而不是
    /var/run/postgresql/.s.PGSQL.5432
    。尝试:

    conn=psycopg2.connect(
      database="mydb",
      user="postgres",
      host="/tmp/",
      password="123"
    )
    

    几年后,使用OSX 10.8上的和PSYOPG2的pip安装(链接了/Library/…dylib之后),尝试将端口更改为5433,而不是5432


    对我来说,正确的connect命令是conn=psycopg2.connect('dbname=dbname-user=postgres-password=PWHERE-host=/tmp/)只有这样才解决了我的问题, 创建到/tmp/.s.PGSQL.5432的符号链接:

    sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432
    

    多亏了,

    我本来打算对他的回答发表评论,但我有很多话要说。。。关于您不直接调用
    psycopg2.connect
    ,而是使用第三方软件的情况

    tl;博士 将
    postgresql.conf
    中的
    unix\u socket\u目录设置为
    /var/run/postgresql,/tmp
    ,然后重新启动postgresql

    简介 我从distro repos试用了PostgreSQL 9.2(CentOS 7)和9.5(Ubuntu Xenial),从CentOS 7试用了PostgreSQL 9.3、9.4、9.5、9.6、10,从Ubuntu Xenial试用了PostgreSQL 9.6、10。其中只有9.3只收听
    /tmp

    $ systemctl stop postgresql-9.4 && systemctl start postgresql-9.3
    $ lsof -aUp $(ps --ppid 1 -o pid= -o comm= | awk '$2 == "postgres" || $2 == "postmaster" {print $1}')
    COMMAND    PID     USER   FD   TYPE             DEVICE SIZE/OFF    NODE NAME
    postgres 25455 postgres    4u  unix 0xffff9acb23bc5000      0t0 6813995 /tmp/.s.PGSQL.5432
    
    $ systemctl stop postgresql-9.3 && systemctl start postgresql-9.4
    $ lsof -aUp $(ps --ppid 1 -o pid= -o comm= | awk '$2 == "postgres" || $2 == "postmaster" {print $1}')
    COMMAND    PID     USER   FD   TYPE             DEVICE SIZE/OFF    NODE NAME
    postgres 26663 postgres    4u  unix 0xffff9ac8c5474c00      0t0 7086508 /var/run/postgresql/.s.PGSQL.5432
    postgres 26663 postgres    5u  unix 0xffff9ac8c5477c00      0t0 7086510 /tmp/.s.PGSQL.5432
    
    python-psycopg2
    这对于psql来说不是什么大问题,只是运行匹配的二进制文件。但如果您从CentOS的
    base
    update
    repo安装了
    python-psycopg2
    。它动态链接到操作系统提供的
    libpq
    。安装了9.3和9.4的操作系统提供了9.4的版本:

    $ alternatives --display pgsql-ld-conf
    pgsql-ld-conf - status is auto.
     link currently points to /usr/pgsql-10/share/postgresql-9.4-libs.conf
    /usr/pgsql-9.3/share/postgresql-9.3-libs.conf - priority 930
    /usr/pgsql-9.4/share/postgresql-9.4-libs.conf - priority 940
    Current `best' version is /usr/pgsql-9.4/share/postgresql-9.4-libs.conf.
    
    $ ls -l /etc/ld.so.conf.d
    lrwxrwxrwx 1 root root 31 Feb  7 02:25 postgresql-pgdg-libs.conf -> /etc/alternatives/pgsql-ld-conf
    
    $ ls -l /etc/alternatives/pgsql-ld-conf
    lrwxrwxrwx 1 root root 43 Feb  7 02:25 /etc/alternatives/pgsql-ld-conf -> /usr/pgsql-9.4/share/postgresql-9.4-libs.conf
    
    $ cat /usr/pgsql-9.4/share/postgresql-9.4-libs.conf
    /usr/pgsql-9.4/lib/
    
    但是PostgreSQL 9.4附带的
    libpq
    /var/run/PostgreSQL
    中查找套接字,而不是在9.3中:

    $ strings /usr/pgsql-9.3/lib/libpq.so.5 | egrep '/(tmp|var)'
    /tmp
    
    $ strings /usr/pgsql-9.4/lib/libpq.so.5 | egrep '/(tmp|var)'
    /var/run/postgresql
    
    解决方案来自相应软件包的安装后脚本:

    $ yum reinstall --downloadonly postgresql94-libs
    $ rpm -qp /var/cache/yum/x86_64/7/pgdg94/packages/postgresql94-libs-9.4.15-1PGDG.rhel7.x86_64.rpm --scripts
    
    postinstall scriptlet (using /bin/sh):
    /usr/sbin/update-alternatives --install /etc/ld.so.conf.d/postgresql-pgdg-libs.conf   pgsql-ld-conf        /usr/pgsql-9.4/share/postgresql-9.4-libs.conf 940
    /sbin/ldconfig                                                                                 
    
    # Drop alternatives entries for common binaries and man files                                  
    postuninstall scriptlet (using /bin/sh):                                                       
    if [ "$1" -eq 0 ]
      then
        /usr/sbin/update-alternatives --remove pgsql-ld-conf /usr/pgsql-9.4/share/postgresql-9.4-libs.conf
        /sbin/ldconfig                                                                             
    fi
    
    暂时删除9.4的替代方案:

    $ alternatives --remove pgsql-ld-conf /usr/pgsql-9.4/share/postgresql-9.4-libs.conf
    $ ldconfig
    
    完成后,请重新安装
    postgresql94 libs
    ,或将替代项添加回:

    $ alternatives --install /etc/ld.so.conf.d/postgresql-pgdg-libs.conf pgsql-ld-conf /usr/pgsql-9.4/share/postgresql-9.4-libs.conf 940
    $ ldconfig
    
    pip
    另一方面,如果您使用
    pip
    安装
    psycopg2
    ,则默认情况下,它会安装预编译包,预编译包附带自己的
    libpq
    ,在
    /var/run/postgresql
    中查找套接字:

    $ python3.5 -m venv 1
    $ . ./1/bin/activate
    (1) $ pip install psycopg2
    
    (1) $ python
    >>> import psycopg2
    >>>Ctrl-Z
    [1]+  Stopped                 python
    
    (1) $ pgrep python
    26311
    
    (1) $ grep libpq /proc/26311/maps | head -n 1
    7f100b8cb000-7f100b90e000 r-xp 00000000 08:04 112980                     /home/yuri/1/lib/python3.5/site-packages/psycopg2/.libs/libpq-909a53d8.so.5.10
    
    (1) $ strings /home/yuri/1/lib/python3.5/site-packages/psycopg2/.libs/libpq-909a53d8.so.5.10 | egrep '/(tmp|var)'
    /var/run
    /var/run/postgresql
    
    解决方案是要求
    pip
    不要安装预编译包,并提供正确版本的PostgreSQL的
    pg_config

    $ PATH=/usr/pgsql-9.3/lib:$PATH pip install --no-binary psycopg2 psycopg2
    
    您甚至可以将
    --无二进制
    开关添加到:

    unix\u socket\u目录

    不过,更简单的选择是使用:

    如果在
    brew升级后发生这种情况,我会在谷歌上搜索
    brew.s.PGSQL.5432

    根据中的建议,我运行了以下程序:

    postgres -D /usr/local/var/postgres
    
    得到:

    2019-10-29 17:43:30.860 IST [78091] FATAL:  database files are incompatible with server
    2019-10-29 17:43:30.860 IST [78091] DETAIL:  The data directory was initialized by PostgreSQL version 10, which is not compatible with this version 11.5.
    
    我在谷歌上搜索了那个致命错误,并根据我运行中的建议:


    这为我解决了问题。

    在我安装conda的情况下,必须:
    sudo ln-s/var/run/postgresql/.s.PGSQL.5432/tmp/.s.PGSQL.5432

    如果将Debian/Ubuntu打包的postgresql安装与手工编译的postgresql安装混合在一起,通常会出现问题。除非您准备好处理此类问题,否则请避免这样做。有没有办法将套接字返回到/var/run/postgresql/?(我刚刚编辑了一个django源文件,以使我的项目暂时重新工作,但这并不是一个好的解决方案)顺便说一句,我不是从源代码安装的……当我用自制的Postgres替换内置的Postgres时,我在OSX上遇到了这个问题。我必须卸载并重新安装psycopg2,使其使用正确的libpq.dylib,而libpq.dylib又使用了正确的unix套接字路径。添加到
    requirements.txt
    并将
    pg_config
    的目录添加到
    PATH
    解决了此问题。同样:host=“/tmp/”未修复,但确实如此。@Dhanalal_Bhardwaj sudo ln-s/tmp/.s.PGSQL.5432/var/run/postgresql/.s.PGSQL.5432 ln:无法访问“/var/run/postgresql/.s.PGSQL.5432”:权限denied@P_M我不知道为什么连sudo也会这样。您可以检查'ln'命令规范和其他与权限相关的答案:|这尤其适用于安装了新的postgres版本而以前的版本仍在运行的情况。由于postgres无法使用端口5432,因此无法连接到下一个可用端口,通常是5433。
    postgres -D /usr/local/var/postgres
    
    2019-10-29 17:43:30.860 IST [78091] FATAL:  database files are incompatible with server
    2019-10-29 17:43:30.860 IST [78091] DETAIL:  The data directory was initialized by PostgreSQL version 10, which is not compatible with this version 11.5.
    
    brew postgresql-upgrade-database