Skip to content

[HELP] PostgreSQL cluster: how to setup more than one host #1072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tyki6 opened this issue Aug 24, 2023 · 1 comment
Closed

[HELP] PostgreSQL cluster: how to setup more than one host #1072

tyki6 opened this issue Aug 24, 2023 · 1 comment

Comments

@tyki6
Copy link

tyki6 commented Aug 24, 2023

Hello,
I request some helm from your side guys.
I created a postgres cluster on aws with one master and one read replica.
My expected goal with asynpg is via url said to asyncpg both host one for write one for read how is it possible?
Regards,

Example

from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker, create_async_engine
postgres_url = "postgresql+asyncpg:master_user:master_password@master_host:5432/mydatabase,replica_user:replica_password@replica_host1:5432/mydatabase
engine_async = create_async_engine(, pool_size=200, max_overflow=10, echo=False)
session_local_async = async_sessionmaker(engine_async, class_=AsyncSession, autoflush=False, expire_on_commit=False)

async def get_db_async() -> AsyncGenerator[AsyncSession, None]:
    async with session_local_async() as session:
        try:
            yield session
        finally:
            await session.close()
  • asyncpg version: 0.28.0
  • PostgreSQL version: 15.0
  • Do you use a PostgreSQL SaaS? If so, which? Can you reproduce
    the issue with a local PostgreSQL install?
    : AWS RDS
  • Python version: 3.11
  • 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?
    :
@elprans
Copy link
Member

elprans commented Aug 24, 2023

The correct DSN would be this (see docs)

postgres_url = "postgresql+asyncpg:master_user:master_password@master_host,replica_host:5432/mydatabase"

You can control whether you need a read-write or read-only connection via the target_session_attrs argument to connect().

@elprans elprans closed this as completed Oct 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants