Skip to main content

Let's Start

...

Installation

You can install psqlpy with pip, poetry or directly from git using pip:

pip
pip install psqlpy

After installation you are ready to start querying!

First request to the database

There is a minimal example of what you need to do to send your first query and receive result. Let's assume that we have table users:

idnameusername
1Aleksandrchandr-andr
2Michailinsani7y
import asyncio
from typing import Final, Any

from psqlpy import ConnectionPool, QueryResult


async def main() -> None:
    # It uses default connection parameters
    db_pool: Final = ConnectionPool()

    async with db_pool.acquire() as conn:
        results: Final[QueryResult] = await conn.execute(
            "SELECT * FROM users WHERE id = $1",
            [2],
        )

    dict_results: Final[list[dict[Any, Any]]] = results.result()
    db_pool.close()

Tips

It's better to call close() on database pool when you application is shutting down.