tortoise / tortoise/tortoise-orm

Database creation breaks if user is not `postgres`

Open
#904 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug
Tortoise.init(*, _create_db=True) suppose to create database. It does create it if username in config is postgres, but it fails if it is not.

To Reproduce
Setup DB with user differenf from postgres. Call Tortoise.init with _create_db=True and config with created user, and not existed database.

Expected behavior
New database created.

Actual behavior
Its trying to connect to database with name as user name and fails as it does not exist.

Additional context
The issue is placed in using asyncpg connections with database=None. The issue source is here - you use asyncpg.create_pull with database=None. Asyncpg in such case take username as value for database name and it works only if username is postgres.

Contributor guide

Open the contributing guide

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 at tortoise/backends/asyncpg/client.py line 127 and trace how Tortoise.init(_create_db=True) opens the asyncpg connection when the configured user is not postgres. Reproduce the setup with a non-postgres user and a missing database; done means database creation succeeds without requiring the username to be the database name.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.