Skip to content

A minimal example to show how to query a collection asynchronously with Psycopg3 and expose those results behind a FastAPI REST endpoint.

License

Notifications You must be signed in to change notification settings

chuck-alt-delete/fastapi_psycopg3_example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FastAPI + Psycopg3 Example

  1. Inspect the code.

  2. Fill out your database connection details as environment variables in a file called .env. Here is an example:

    export MZ_HOST=<id>.<region>.aws.materialize.cloud
    export [email protected]
    export MZ_PASSWORD=<app password>
    export MZ_PORT=6875
    export MZ_DB=materialize
    export MZ_CLUSTER=chuck
    export MZ_SCHEMA=public
    export MZ_TRANSACTION_ISOLATION=serializable
    
  3. Install dependencies.

    python3 -m venv .venv
    source .venv/bin/activate
    pip install -r requirements.txt
  4. Run the FastAPI server.

    uvicorn main:app
  5. Access the endpoint.

    curl -L localhost:8000/my_data
    {"data":[{"a":0,"b":0},{"a":0,"b":1},{"a":1,"b":0},{"a":1,"b":1},{"a":2,"b":0}]}

    This assumes you have a Postgres compatible database with a collection called my_collection with integer columns a and b.

About

A minimal example to show how to query a collection asynchronously with Psycopg3 and expose those results behind a FastAPI REST endpoint.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages