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

from psqlpy import ConnectionPool, QueryResult


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

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

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

Tips

You must call close() on database pool when you application is shutting down.

Caution

You must not use ConnectionPool.execute method in high-load production code! It pulls new connection from connection pull each call. Recommended way to make queries is executing them with Connection, Transaction or Cursor.