[Python] When using Tortoise with Python, a database connection refused error occurred

Discussão em 'Python' iniciado por Stack, Abril 4, 2025.

  1. Stack

    Stack Membro Participativo

    I have defined several tortoise models, but the tables are not being created in the database.The Tortoise-related code was downloaded from GitHub (production version), so it should be correct. I don't think you need to worry about it.

    I'm a beginner—could you please help me identify what might be wrong?

    [Environment & Technologies Used]

    Docker
    Python
    Node.js
    Vue3
    fastapi
    tortoise-orm


    【source】 ■ docker-compose.yml

    services:
    backend:
    ...
    environment:
    - DATABASE_URL=postgres://pName:ppwd@db:5432/dbdev
    volumes:
    - ./backend:/app
    command: uvicorn main:app --reload --host 0.0.0.0 --port 5000
    depends_on:
    - db
    ...
    db:
    image: postgres:15.1
    ports:
    - "5433:5432"
    environment:
    - POSTGRES_USER=pName
    - POSTGRES_PASSWORD=ppwd
    - POSTGRES_DB=dbdev


    ■ TORTOISE_ORM partial code

    TORTOISE_ORM = {
    "connections": {"default": os.environ.get("DATABASE_URL")},
    "apps": {
    "models": {
    "models": [
    "database.models", "aerich.models"
    ],
    "default_connection": "default"
    }
    }
    }


    When checked on the local Windows environment, the connection was successful.

    C:\Program Files\PostgreSQL\17\bin>psql postgresql://pName:ppwd@localhost:5433/dbdev
    psql (17.4、server 15.1 (Debian 15.1-1.pgdg110+1))

    dbdev=# \q


    ■docker backend error log

    2025-04-02 09:15:08 INFO: Will watch for changes in these directories: ['/app']
    2025-04-02 09:15:08 INFO: Uvicorn running on http://0.0.0.0:5000 (Press CTRL+C to quit)
    2025-04-02 09:15:08 INFO: Started reloader process [1] using WatchFiles
    2025-04-02 09:15:09 INFO: Started server process [8]
    2025-04-02 09:15:09 INFO: Waiting for application startup.
    2025-04-02 09:15:09 2025-04-02 00:15:09 DEBUG Tortoise-ORM startup
    2025-04-02 09:15:09 connections: {'default': 'postgres://pName:p***@db:5432/dbdev'}
    2025-04-02 09:15:09 apps: {'models': {'models': ['database.models', 'aerich.models'], 'default_connection': 'default'}}
    2025-04-02 09:15:09 2025-04-02 00:15:09 DEBUG Creating schema: CREATE TABLE IF NOT EXISTS "user" (
    2025-04-02 09:15:09 "id" SERIAL NOT NULL PRIMARY KEY,
    2025-04-02 09:15:09 "username" VARCHAR(255) NOT NULL UNIQUE,
    2025-04-02 09:15:09 "password" VARCHAR(128),
    2025-04-02 09:15:09 "openai_api_key" VARCHAR(128),
    2025-04-02 09:15:09 "last_login_at" TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    2025-04-02 09:15:09 "created_at" TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    2025-04-02 09:15:09 "modified_at" TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
    2025-04-02 09:15:09 );
    2025-04-02 09:15:09 CREATE TABLE IF NOT EXISTS "usages" (
    2025-04-02 09:15:09 "id" SERIAL NOT NULL PRIMARY KEY,
    2025-04-02 09:15:09 "month" VARCHAR(8) NOT NULL,
    2025-04-02 09:15:09 "successful_amount" BIGINT NOT NULL,
    2025-04-02 09:15:09 "failure_amount" BIGINT NOT NULL,
    2025-04-02 09:15:09 "created_at" TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    2025-04-02 09:15:09 "modified_at" TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
    2025-04-02 09:15:09 "user_id" INT NOT NULL REFERENCES "user" ("id") ON DELETE CASCADE
    2025-04-02 09:15:09 );
    2025-04-02 09:15:09 CREATE TABLE IF NOT EXISTS "aerich" (
    2025-04-02 09:15:09 "id" SERIAL NOT NULL PRIMARY KEY,
    2025-04-02 09:15:09 "version" VARCHAR(255) NOT NULL,
    2025-04-02 09:15:09 "app" VARCHAR(100) NOT NULL,
    2025-04-02 09:15:09 "content" JSONB NOT NULL
    2025-04-02 09:15:09 );
    2025-04-02 09:15:09 ERROR: Traceback (most recent call last):
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 693, in lifespan
    2025-04-02 09:15:09 async with self.lifespan_context(app) as maybe_state:
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/contextlib.py", line 199, in __aenter__
    2025-04-02 09:15:09 return await anext(self.gen)
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 133, in merged_lifespan
    2025-04-02 09:15:09 async with original_context(app) as maybe_original_state:
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/contextlib.py", line 199, in __aenter__
    2025-04-02 09:15:09 return await anext(self.gen)
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/fastapi/routing.py", line 133, in merged_lifespan
    2025-04-02 09:15:09 async with original_context(app) as maybe_original_state:
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 569, in __aenter__
    2025-04-02 09:15:09 await self._router.startup()
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/starlette/routing.py", line 670, in startup
    2025-04-02 09:15:09 await handler()
    2025-04-02 09:15:09 File "/app/database/register.py", line 16, in init_orm
    2025-04-02 09:15:09 await Tortoise.generate_schemas()
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/tortoise/__init__.py", line 584, in generate_schemas
    2025-04-02 09:15:09 await generate_schema_for_client(connection, safe)
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/tortoise/utils.py", line 43, in generate_schema_for_client
    2025-04-02 09:15:09 await generator.generate_from_string(schema)
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/tortoise/backends/base/schema_generator.py", line 487, in generate_from_string
    2025-04-02 09:15:09 await self.client.execute_script(creation_string)
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/tortoise/backends/base_postgres/client.py", line 41, in _translate_exceptions
    2025-04-02 09:15:09 return await self._translate_exceptions(func, *args, **kwargs)
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/tortoise/backends/asyncpg/client.py", line 87, in _translate_exceptions
    2025-04-02 09:15:09 return await func(self, *args, **kwargs)
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/tortoise/backends/base_postgres/client.py", line 157, in execute_script
    2025-04-02 09:15:09 async with self.acquire_connection() as connection:
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/tortoise/backends/base/client.py", line 341, in __aenter__
    2025-04-02 09:15:09 await self.ensure_connection()
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/tortoise/backends/base/client.py", line 337, in ensure_connection
    2025-04-02 09:15:09 await self.client.create_connection(with_db=True)
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/tortoise/backends/asyncpg/client.py", line 59, in create_connection
    2025-04-02 09:15:09 self._pool = await self.create_pool(password=self.password, **self._template)
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/tortoise/backends/asyncpg/client.py", line 70, in create_pool
    2025-04-02 09:15:09 return await asyncpg.create_pool(None, **kwargs)
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/asyncpg/pool.py", line 418, in _async__init__
    2025-04-02 09:15:09 await self._initialize()
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/asyncpg/pool.py", line 445, in _initialize
    2025-04-02 09:15:09 await first_ch.connect()
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/asyncpg/pool.py", line 132, in connect
    2025-04-02 09:15:09 self._con = await self._pool._get_new_connection()
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/asyncpg/pool.py", line 517, in _get_new_connection
    2025-04-02 09:15:09 con = await self._connect(
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/asyncpg/connection.py", line 2421, in connect
    2025-04-02 09:15:09 return await connect_utils._connect(
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/asyncpg/connect_utils.py", line 1075, in _connect
    2025-04-02 09:15:09 raise last_error or exceptions.TargetServerAttributeNotMatched(
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/asyncpg/connect_utils.py", line 1049, in _connect
    2025-04-02 09:15:09 conn = await _connect_addr(
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/asyncpg/connect_utils.py", line 886, in _connect_addr
    2025-04-02 09:15:09 return await __connect_addr(params, True, *args)
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/asyncpg/connect_utils.py", line 931, in __connect_addr
    2025-04-02 09:15:09 tr, pr = await connector
    2025-04-02 09:15:09 File "/usr/local/lib/python3.10/site-packages/asyncpg/connect_utils.py", line 802, in _create_ssl_connection
    2025-04-02 09:15:09 tr, pr = await loop.create_connection(
    2025-04-02 09:15:09 File "uvloop/loop.pyx", line 2043, in create_connection
    2025-04-02 09:15:09 File "uvloop/loop.pyx", line 2020, in uvloop.loop.Loop.create_connection
    2025-04-02 09:15:09 ConnectionRefusedError: [Errno 111] Connection refused
    2025-04-02 09:15:09
    2025-04-02 09:15:09 ERROR: Application startup failed. Exiting.

    Continue reading...

Compartilhe esta Página