How to use non-async PGEngine? #98

Open
opened 2026-02-16 05:16:32 -05:00 by yindo · 1 comment
Owner

Originally created by @vlavorini on GitHub (Jan 24, 2026).

Originally assigned to: @dishaprakash on GitHub.

In the notebooks is wrote that all the methods have equivalent sync ones. But I see in the definition of PGEngine that only async is accepted:

class PGEngine:
    """A class for managing connections to a Postgres database."""

    _default_loop: Optional[asyncio.AbstractEventLoop] = None
    _default_thread: Optional[Thread] = None
    __create_key = object()

    def __init__(
        self,
        key: object,
        pool: AsyncEngine,
        loop: Optional[asyncio.AbstractEventLoop],
        thread: Optional[Thread],
    ) -> None:
Originally created by @vlavorini on GitHub (Jan 24, 2026). Originally assigned to: @dishaprakash on GitHub. In the notebooks is wrote that all the methods have equivalent sync ones. But I see in the definition of PGEngine that only async is accepted: ```python class PGEngine: """A class for managing connections to a Postgres database.""" _default_loop: Optional[asyncio.AbstractEventLoop] = None _default_thread: Optional[Thread] = None __create_key = object() def __init__( self, key: object, pool: AsyncEngine, loop: Optional[asyncio.AbstractEventLoop], thread: Optional[Thread], ) -> None: ```
Author
Owner

@dishaprakash commented on GitHub (Jan 26, 2026):

Hi @vlavorini !

You can create an instance of PGEngine by using this code:

from langchain_postgres import PGEngine

CONNECTION_STRING = (
    f"postgresql+asyncpg://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}"
    f":{POSTGRES_PORT}/{POSTGRES_DB}"
)
# To use psycopg3 driver, set your connection string to `postgresql+psycopg://`

pg_engine = PGEngine.from_connection_string(url=CONNECTION_STRING)

After creating the PGEngine, you can initialize a vector store table in your database using either the synchronous init_vectorstore_table() method or its asynchronous counterpart, ainit_vectorstore_table().

Similarly, the PGVectorStore class offers both sync and async methods to support your workflow. For more info refer to the how-to guide

@dishaprakash commented on GitHub (Jan 26, 2026): Hi @vlavorini ! You can create an instance of PGEngine by using this code: ``` from langchain_postgres import PGEngine CONNECTION_STRING = ( f"postgresql+asyncpg://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}" f":{POSTGRES_PORT}/{POSTGRES_DB}" ) # To use psycopg3 driver, set your connection string to `postgresql+psycopg://` pg_engine = PGEngine.from_connection_string(url=CONNECTION_STRING) ``` After creating the PGEngine, you can initialize a vector store table in your database using either the synchronous `init_vectorstore_table()` method or its asynchronous counterpart, `ainit_vectorstore_table()`. Similarly, the PGVectorStore class offers both sync and async methods to support your workflow. For more info refer to the [how-to guide](https://github.com/langchain-ai/langchain-postgres/blob/main/examples/pg_vectorstore_how_to.ipynb)
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/langchain-postgres#98