postgresqlfastapiasyncpgtortoise-orm

How to solve asyncpg.exceptions.InvalidAuthorizationSpecificationError


I'm going through the course Test-Driven Development with FastAPI and Docker from testdriven.io and just stucked with releasing my app to heroku (Part 2: Deployment). Everything was fine before I released the image to heroku:

heroku container:release web --app APP_NAME

Then I checked the https://APP_NAME.herokuapp.com/ping/ endpoint and got

503 Service Unavailable.

Need help. I found such tracebacks in heroku logs --tail:

2021-02-09T12:37:53.995055+00:00 app[web.1]: [2021-02-09 12:37:53 +0000] [27] [ERROR] Application startup failed. Exiting.
2021-02-09T12:37:53.995458+00:00 app[web.1]: [2021-02-09 12:37:53 +0000] [27] [INFO] Worker exiting (pid: 27)
2021-02-09T12:37:54.123770+00:00 app[web.1]: [2021-02-09 12:37:54 +0000] [32] [INFO] Booting worker with pid: 32
2021-02-09T12:37:54.773146+00:00 app[web.1]: [2021-02-09 12:37:54 +0000] [32] [INFO] Started server process [32]
2021-02-09T12:37:54.773392+00:00 app[web.1]: [2021-02-09 12:37:54 +0000] [32] [INFO] Waiting for application startup.
2021-02-09T12:37:54.885999+00:00 app[web.1]: [2021-02-09 12:37:54 +0000] [32] [ERROR] Traceback (most recent call last):
2021-02-09T12:37:54.886001+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 526, in lifespan
2021-02-09T12:37:54.886002+00:00 app[web.1]: async for item in self.lifespan_context(app):
2021-02-09T12:37:54.886003+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 467, in default_lifespan
2021-02-09T12:37:54.886004+00:00 app[web.1]: await self.startup()
2021-02-09T12:37:54.886004+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/starlette/routing.py", line 502, in startup
2021-02-09T12:37:54.886004+00:00 app[web.1]: await handler()
2021-02-09T12:37:54.886005+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/tortoise/contrib/fastapi/__init__.py", line 92, in init_orm
2021-02-09T12:37:54.886006+00:00 app[web.1]: await Tortoise.init(config=config, config_file=config_file, db_url=db_url, modules=modules)
2021-02-09T12:37:54.886006+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/tortoise/__init__.py", line 567, in init
2021-02-09T12:37:54.886007+00:00 app[web.1]: await cls._init_connections(connections_config, _create_db)
2021-02-09T12:37:54.886007+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/tortoise/__init__.py", line 385, in _init_connections
2021-02-09T12:37:54.886008+00:00 app[web.1]: await connection.create_connection(with_db=True)
2021-02-09T12:37:54.886008+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/tortoise/backends/asyncpg/client.py", line 94, in create_connection
2021-02-09T12:37:54.886009+00:00 app[web.1]: self._pool = await asyncpg.create_pool(None, password=self.password, **self._template)
2021-02-09T12:37:54.886009+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/asyncpg/pool.py", line 398, in _async__init__
2021-02-09T12:37:54.886010+00:00 app[web.1]: await self._initialize()
2021-02-09T12:37:54.886010+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/asyncpg/pool.py", line 426, in _initialize
2021-02-09T12:37:54.886010+00:00 app[web.1]: await first_ch.connect()
2021-02-09T12:37:54.886011+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/asyncpg/pool.py", line 125, in connect
2021-02-09T12:37:54.886011+00:00 app[web.1]: self._con = await self._pool._get_new_connection()
2021-02-09T12:37:54.886012+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/asyncpg/pool.py", line 468, in _get_new_connection
2021-02-09T12:37:54.886012+00:00 app[web.1]: con = await connection.connect(
2021-02-09T12:37:54.886012+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/asyncpg/connection.py", line 1718, in connect
2021-02-09T12:37:54.886013+00:00 app[web.1]: return await connect_utils._connect(
2021-02-09T12:37:54.886013+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/asyncpg/connect_utils.py", line 663, in _connect
2021-02-09T12:37:54.886014+00:00 app[web.1]: con = await _connect_addr(
2021-02-09T12:37:54.886014+00:00 app[web.1]: File "/usr/local/lib/python3.8/site-packages/asyncpg/connect_utils.py", line 642, in _connect_addr
2021-02-09T12:37:54.886014+00:00 app[web.1]: await asyncio.wait_for(connected, timeout=timeout)
2021-02-09T12:37:54.886015+00:00 app[web.1]: File "/usr/local/lib/python3.8/asyncio/tasks.py", line 491, in wait_for
2021-02-09T12:37:54.886015+00:00 app[web.1]: return fut.result()
2021-02-09T12:37:54.886016+00:00 app[web.1]: asyncpg.exceptions.InvalidAuthorizationSpecificationError: no pg_hba.conf entry for host "XXX.XXX.XXX.XXX", user "XXXXXXXXXX", database "XXXXXXXXXXXX", SSL off

Solution

  • Try updating the following dependencies:

    asyncpg==0.22.0
    fastapi==0.63.0
    requests==2.25.1
    tortoise-orm==0.16.21
    

    https://github.com/testdrivenio/fastapi-tdd-docker/issues/5#issuecomment-798435535