Skip to content

Instantly share code, notes, and snippets.

@chandra-prakash-meghwal
Created November 1, 2022 08:36
Show Gist options
  • Save chandra-prakash-meghwal/65ab2f792beac489cd3034d0cedc5101 to your computer and use it in GitHub Desktop.
Save chandra-prakash-meghwal/65ab2f792beac489cd3034d0cedc5101 to your computer and use it in GitHub Desktop.
connect to postgres database using asyncpg
import asyncpg
db_config = {
'user': 'postgres', # name of db user
'password': 'my db password', # db password
'database': 'my_db', # database name
'host': 'localhost' # or remote db ip address
}
async def custom_query(query_string):
conn = await asyncpg.connect(**db_config)
values = await conn.fetch('''{}'''.format(query_string))
await conn.close()
return values
# to execute a query, custom_query("select * from my_table")
# where my_table is the name of the table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment