Skip to content Skip to sidebar Skip to footer

How Do I Connect To Postgresql Using Ssl From Sqlachemy+pg8000?

Connecting to postgres via pg8000 from SqlAlchemy worked fine until I enabled SSL on postgres. db = create_engine('postgresql+pg8000://user:pass@hostname/dbname', echo=True).connec

Solution 1:

probably you need to add connect_args dict:

db = create_engine('postgresql+pg8000://user:pass@hostname/dbname', connect_args={'sslmode':'require'}, echo=True).connect()

Solution 2:

The accepted answer no longer works, at least with these versions:

Python                   3.9
pg8000                   1.19.5
SQLAlchemy               1.4.12

The pg8000 docs describe what you have to do. Use

engine = create_engine('postgresql+pg8000://user:password@host/db', 
                       connect_args={'ssl_context': True})

which passes the result of ssl.create_default_context() to the connection creator. If a custom SSL context is required, pass it as the value instead of True:

import ssl

ssl_context = ssl.SSLContext()
# Set attributes as required  ...

engine = create_engine('postgresql+pg8000://user:password@host/db', 
                       connect_args={'ssl_context': ssl_context})

Solution 3:

Didn't work for me... I used:

engine = create_engine('postgresql+pg8000://user:password@host/db', connect_args={'ssl': {'ssl-mode': 'required'}})

Post a Comment for "How Do I Connect To Postgresql Using Ssl From Sqlachemy+pg8000?"