Skip to content

Connecting via default unix socket without username/password #81

@phdru

Description

@phdru

Hello! I have the following settings in my pg_hba.conf:

local   all             all                                     peer
host    all             all             127.0.0.1/32            md5

That allows me to connect to any database via unix socket without authentication, but when I connect via localhost I am asked for password.

I.e., I can run

$ createdb test
$ psql test
connected

and be connected. Please note that by default psql (libpq, perhaps?) connects via unix socket; also note psql doesn't require username. If I want to connect via localhost I have to do it explicitly:

$ psql -h localhost test
Password:

I can also connect via non-standard unix socket, the option is also -h host:

$ psql -h /run/postgresql test
connected

Only directory is required. Driver psycopg2 follows the conventions (perhaps because they are imposed by libpq and psycopg2 uses libpq):

con = psycopg2.connect(database="test")  # connected
con = psycopg2.connect(database="test", host='/run/postgresql')  # connected

Unfortunately, py-postgresql doesn't follow these conventions:

from postgresql.driver import dbapi20
con = dbapi20.connect(database="test")

Traceback (most recent call last):
  File "/home/phd/.tox/SQLObject/py34-pypostgresql/lib/python3.4/site-packages/postgresql/driver/pq3.py", line 2422, in connect
    self._establish()
  File "/home/phd/.tox/SQLObject/py34-pypostgresql/lib/python3.4/site-packages/postgresql/driver/pq3.py", line 2548, in _establish
    self.typio.raise_client_error(could_not_connect, creator = self, cause = exc)
  File "/home/phd/.tox/SQLObject/py34-pypostgresql/lib/python3.4/site-packages/postgresql/driver/pq3.py", line 514, in raise_client_error
    raise client_error
postgresql.exceptions.ClientCannotConnectError: could not establish connection to server
  CODE: 08001
  LOCATION: CLIENT
CONNECTION: [failed]
  failures[0]:
    SSL socket('127.0.0.1', 5432)
    postgresql.exceptions.AuthenticationSpecificationError: password authentication failed for user "phd"
      CODE: 28P01
      LOCATION: File 'auth.c', line 285, in auth_failed from SERVER
  failures[1]:
    socket('127.0.0.1', 5432)
    postgresql.exceptions.AuthenticationSpecificationError: password authentication failed for user "phd"
      CODE: 28P01
      LOCATION: File 'auth.c', line 285, in auth_failed from SERVER
CONNECTOR: [Host] pq://phd@localhost:5432/test
  category: None
DRIVER: postgresql.driver.pq3.Driver

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./test_pypostgresql.py", line 5, in <module>
    con = dbapi20.connect(database="test")
  File "/home/phd/.tox/SQLObject/py34-pypostgresql/lib/python3.4/site-packages/postgresql/driver/dbapi20.py", line 409, in connect
    return driver.connect(**params)
  File "/home/phd/.tox/SQLObject/py34-pypostgresql/lib/python3.4/site-packages/postgresql/driver/pq3.py", line 3032, in connect
    c.connect()
  File "/home/phd/.tox/SQLObject/py34-pypostgresql/lib/python3.4/site-packages/postgresql/driver/dbapi20.py", line 354, in connect
    super().connect(*args, **kw)
  File "/home/phd/.tox/SQLObject/py34-pypostgresql/lib/python3.4/site-packages/postgresql/driver/pq3.py", line 2425, in connect
    self.close()
  File "/home/phd/.tox/SQLObject/py34-pypostgresql/lib/python3.4/site-packages/postgresql/driver/dbapi20.py", line 363, in close
    creator = self
postgresql.exceptions.Error: connection already closed
  LOCATION: CLIENT
CONNECTION: [failed]
  failures[0]:
    SSL socket('127.0.0.1', 5432)
    postgresql.exceptions.AuthenticationSpecificationError: password authentication failed for user "phd"
      CODE: 28P01
      LOCATION: File 'auth.c', line 285, in auth_failed from SERVER
  failures[1]:
    socket('127.0.0.1', 5432)
    postgresql.exceptions.AuthenticationSpecificationError: password authentication failed for user "phd"
      CODE: 28P01
      LOCATION: File 'auth.c', line 285, in auth_failed from SERVER
CONNECTOR: [Host] pq://phd@localhost:5432/test
  category: None
DRIVER: postgresql.driver.pq3.Driver

Seems like I'm connecting via localhost, not unix socket.

from postgresql.driver import dbapi20
con = dbapi20.connect(host=None, port=None,
    unix='/var/run/postgresql/.s.PGSQL.5432', database="test")  # connected

Can py-postgresql be fixed so that

con = dbapi20.connect(database="test")

connects via default unix socket, without username/password?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions