Example how to use Python Sanic and Postgre Asyncpg
- Install python sanic
$ pip install sanic
- Install PostgreSQL Database Client
$ pip install asyncpg
- Create conection pool
from asyncpg import create_pool
@app.listener('before_server_start')
async def register_db(app, loop):
# Create a database connection pool
conn = "postgres://{user}:{password}@{host}:{port}/{database}".format(
user='postgres', password='secret', host='localhost',
port=5432, database='some_database'
)
app.config['pool'] = await create_pool(
dsn=conn,
min_size=10, #in bytes,
max_size=10, #in bytes,
max_queries=50000,
max_inactive_connection_lifetime=300,
loop=loop)
- To see detail code you can check
main.py
- Happy coding :)