web-dev-qa-db-ja.com

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 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. 私のpostgreSQLは実行中です
  2. 私のリスニングポートは確かに5432です
  3. root @ lanston-laptop:〜#psql -lパスワード:
データベースのリスト
名前|オーナー|エンコード|照合| Ctype |アクセス権
 --------------- + ---------- + ---------- + ----- ------- + ------------ + ----------------------- 
 checkdatabase |ポストグレス| UTF8 | en_US.utf8 | en_US.utf8 | 
 mydb |ポストグレス| UTF8 | en_US.utf8 | en_US.utf8 | 
 postgres |ポストグレス| UTF8 | en_US.utf8 | en_US.utf8 | 
 template0 |ポストグレス| UTF8 | en_US.utf8 | en_US.utf8 | = c/postgres + 
 | | | | | postgres = CTc/postgres 
 template1 |ポストグレス| UTF8 | en_US.utf8 | en_US.utf8 | = c/postgres + 
 | | | | | postgres = CTc/postgres 

どうもありがとう!

33
Lanston

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"
)
54
Tometzky

ポートを5432ではなく5433に変更してみてください

6
radeklos

これだけが私の問題を解決し、/ tmp/.s.PGSQL.5432へのシンボリックリンクを作成します。

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

おかげで、 Sukhjit Singh Sehra-s-postgresql-server-is-running

4

数年後、OSX 10.8で EnterpriseDB 'graphical' install を使用し、psycopg2のpipインストール(/Library/...dylibをリンクした後 ここで説明 )これと同じ問題がありました。

私にとって正しい接続コマンドはconn = psycopg2.connect('dbname=DBNAME user=postgres password=PWHERE Host=/tmp/')でした

3
rikb

私はもともと Tometzky の答えにコメントを付けるつもりでしたが、よくここで言いたいことがたくさんあります... psycopg2.connectを直接呼び出さない場合については、ただし、サードパーティのソフトウェアを使用します。

tl; dr

unix_socket_directoriespostgresql.conf/var/run/postgresql, /tmpに設定し、PostgreSQLを再起動します。

イントロ

PostgreSQL 9.2(CentOS 7)および9.5(Ubuntu Xenial)をディストリビューションリポジトリから、PostgreSQL 9.3、9.4、9.5、9.6、10をCentOS 7で PostgreSQLリポジトリ から、PostgreSQL 9.6、10をUbuntu Xenialで試しました PostgreSQLリポジトリ 。そのうち、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とは大したことではなく、単に一致するバイナリを実行するだけです。しかし、たとえば、python-psycopg2がCentOSのbaseまたはupdateリポジトリからインストールされている場合。 OSが提供するlibpqに動的にリンクします。 9.3および9.4をインストールすると、OSは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は、9.3ではなく/var/run/postgresqlでソケットを探します。

$ 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

一方、psycopg2pipとともにインストールすると、デフォルトで、/var/run/postgresqlでソケットを探す独自のlibpqが付属するプリコンパイル済みパッケージをインストールします。

$ 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

--no-binaryスイッチを requirements.txt に追加することもできます。

psycopg2==2.7.3.2 --no-binary psycopg2

unix_socket_directories

ただし、より簡単なオプションは unix_socket_directories option を使用することです。

1
x-yuri