MagicStack / MagicStack/asyncpg

asyncpg does not work with "sslmode" query param when called from SQLAlchemy

Open
#737 18 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
8.1k
Forks
468
PR merge metrics
No merged PRs in 30d

Description

* **asyncpg version**: 0.22
* **PostgreSQL version**: 12.3
* **Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
the issue with a local PostgreSQL install?**: I use DigitalOcean and was able to also reproduce it locally
* **Python version**: 3.8.9
* **Platform**: Linux
* **Do you use pgbouncer?**: No
* **Did you install asyncpg with pip?**: Yes
* **If you built asyncpg locally, which version of Cython did you use?**: -
* **Can the issue be reproduced under both asyncio and
[uvloop](https://github.com/magicstack/uvloop)?**: N/D

First, congratulation to the people of MagicStack for this great library.

When using DigitalOcean default environment DATABASE URL to set the database string on my app I got the following error:
```
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/pool/base.py", line 599, in __connect
connection = pool._invoke_creator(self)
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/create.py", line 578, in connect
return dialect.connect(*cargs, **cparams)
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 558, in connect
return self.dbapi.connect(*cargs, **cparams)
File "/usr/local/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/asyncpg.py", line 747, in connect
await_only(self.asyncpg.connect(*arg, **kw)),
TypeError: connect() got an unexpected keyword argument 'sslmode'
```

In my app, since I use `psycopg2` to do some "management tasks", I also need to do something like that:
```
if ssl_enabled and driver == "asyncpg":
base_url += "?ssl=require"
elif ssl_enabled and driver == "psycopg2":
base_url += "?sslmode=require"
```

Shouldn't `asyncpg` use the more standard `sslmode=` query param instead of the `ssl=`?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the SQLAlchemy PostgreSQL asyncpg dialect path shown in the traceback, especially sqlalchemy/dialects/postgresql/asyncpg.py, and trace how the URL query parameters reach asyncpg.connect. Reproduce the failure with sslmode=require and compare it with ssl=require. Done means the intended parameter works through SQLAlchemy without the unexpected-keyword error, with coverage for the connection URL case.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python, sqlalchemy
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.