How do I ensure that my SQLAlchemy connection to PostgreSQL is over a Unix socket or even IPv4? It will only connect via IPv6.
I configured create_engine as @localhost but note the host IPv6 host "::1" failure at the bottom. I purposely removed the IPv6 trust in pg_hba.conf (see below) to show the error. If I uncomment that line this will connect successfully over IPv6 and skip Unix and IPv4.
>>> from sqlalchemy import *
db = create_engine('postgresql://vaderade@localhost:5432/mydb', echo=True)
>>> metadata = MetaData(db)
>>> users = Table('users', metadata,
... Column('user_id', Integer, primary_key=True),
... Column('name', String(40)),
... Column('age', Integer),
... Column('password', String),
... )
>>> users.create()
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/pool.py", line 1044, in _do_get
return self._pool.get(wait, self._timeout)
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/util/queue.py", line 145, in get
raise Empty
sqlalchemy.util.queue.Empty
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/engine/base.py", line 2074, in _wrap_pool_connect
return fn()
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/pool.py", line 376, in connect
return _ConnectionFairy._checkout(self)
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/pool.py", line 713, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/pool.py", line 480, in checkout
rec = pool._do_get()
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/pool.py", line 1060, in _do_get
self._dec_overflow()
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
raise value
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/pool.py", line 1057, in _do_get
return self._create_connection()
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/pool.py", line 323, in _create_connection
return _ConnectionRecord(self)
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/pool.py", line 449, in __init__
self.connection = self.__connect()
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/pool.py", line 607, in __connect
connection = self.__pool._invoke_creator(self)
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/engine/strategies.py", line 97, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/lib/python3.5/site-packages/sqlalchemy/engine/default.py", line 385, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/lib/python3.5/site-packages/psycopg2/__init__.py", line 164, in connect
conn = _connect(dsn, connection_factory=connection_factory, async=async)
psycopg2.OperationalError: FATAL: no pg_hba.conf entry for host "::1", user "vaderade", database "mydb", SSL off
Relevant info from /var/lib/pgsql/9.5/data/pg_hba.conf
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
#host all all ::1/128 trust
Relevant info from /var/lib/pgsql/9.5/data/postgresql.conf
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------
# - Connection Settings -
listen_addresses = 'localhost' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
Specifics:
- Red Hat 7.2
- PostgreSQL 9.5.4
- Python 3.5.2
- SQLAlchemy==1.0.14
- psycopg2==2.6.2
127.0.0.1instead oflocalhost. - univerio