Can't connect the postgreSQL with psycopg2
这是我第一次找不到关于某些技术问题的答案
这是我的问题:
1 2 3 4 5 6 7 | >> 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"? |
密码:
1 2 3 4 5 6 7 8 9 10 | List OF DATABASES Name | Owner | Encoding | Collation | Ctype | Access privileges ---------------+----------+----------+------------+------------+----------------------- checkdatabase | postgres | UTF8 | en_US.utf8 | en_US.utf8 | mydb | postgres | UTF8 | en_US.utf8 | en_US.utf8 | postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 | template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres + | | | | | postgres=CTc/postgres |
非常感谢!
psycopg2使用的libpq要求Postgres套接字在
检查是否有文件
1 2 3 4 5 6 | conn=psycopg2.connect( DATABASE="mydb", USER="postgres", host="/tmp/", password="123" ) |
尝试将端口更改为5433而不是5432
只有这解决了我的问题,
创建一个指向/tmp/.s.PGSQL.5432的符号链接:
1 | sudo ln -s /tmp/.s.PGSQL.5432 /var/run/postgresql/.s.PGSQL.5432 |
感谢Sukhjit Singh Sehra - s-postgresql-server-is-running
几年后,使用OSX 10.8上的EnterpriseDB'图形'安装,以及psycopg2的pip安装(在链接如此处所述的/Library/...dylib之后),我遇到了同样的问题。
对我来说,正确的连接命令是
我原本打算对Tometzky的回答发表评论,但是,我在这里有很多话要说......关于你不直接调用
TL;博士
将
介绍
我从PostgreSQL repo上发布了PostgreSQL 9.2(CentOS 7)和9.5(Ubuntu Xenial),来自PostgreSQL repo的PostOSSQL 9.3,9.4,9.5,9.6,10,PostgreSQL 9.6,10,来自PostgreSQL repo的Ubuntu Xenial。其中只有9.3只侦听
1 2 3 4 5 6 7 8 9 10 | $ 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 |
这对于
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | $ 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附带的
1 2 3 4 5 | $ 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 |
解决方案来自相应软件包的postinstall脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $ 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的替代方案:
1 2 | $ alternatives --remove pgsql-ld-conf /usr/pgsql-9.4/share/postgresql-9.4-libs.conf $ ldconfig |
完成后重新安装
1 2 | $ 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 |
另一方面,如果使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | $ 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 |
解决方案是要求
1 | $ PATH=/usr/pgsql-9.3/lib:$PATH pip install --no-binary psycopg2 psycopg2 |
您甚至可以将
1 | psycopg2==2.7.3.2 --no-binary psycopg2 |
<5233>
但更简单的选择是使用