Skip to content

Instantly share code, notes, and snippets.

@fliepeltje
Created July 25, 2024 09:47
Show Gist options
  • Save fliepeltje/5a2b63d4b1169f5cb200a665cee3f926 to your computer and use it in GitHub Desktop.
Save fliepeltje/5a2b63d4b1169f5cb200a665cee3f926 to your computer and use it in GitHub Desktop.
FastAPI app with asyncpg minimal example
import os
import asyncpg
from fastapi import FastAPI
DATABASE_URL = os.getenv("DATABASE_URL")
app = FastAPI()
async def get_db_meta():
conn = await asyncpg.connect(DATABASE_URL)
records = await conn.fetch("select nspname from pg_namespace;")
return records
@app.get("/")
async def read_root():
db_meta = await get_db_meta()
names = [record["nspname"] for record in db_meta]
return {"names": names}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment