Skip to content

Instantly share code, notes, and snippets.

@iKlotho
Created February 3, 2020 08:30
Show Gist options
  • Save iKlotho/37da0fe6d0a76ea60db7977f90928ce0 to your computer and use it in GitHub Desktop.
Save iKlotho/37da0fe6d0a76ea60db7977f90928ce0 to your computer and use it in GitHub Desktop.
fastapi - asyncpg
async def setup_database():
global connection_pool
connection_pool = await asyncpg.create_pool(
user=os.getenv('PGUSER', 'x'),
password=os.getenv('PGPASS', 'xx'),
database='xx',
host='xxx',
port=5432
)
connection_pool = None
app = FastAPI()
@app.on_event("startup")
async def startup():
await setup_database()
@app.get("/ir/", response_model=List[Note])
async def read_notes():
async with connection_pool.acquire() as connection:
await connection.execute("SET search_path TO 'xx'")
data = await connection.fetch(READ_IR_SQL)
print("data", data)
return {'data': 'gee'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment