Created
November 1, 2022 08:36
-
-
Save chandra-prakash-meghwal/65ab2f792beac489cd3034d0cedc5101 to your computer and use it in GitHub Desktop.
connect to postgres database using asyncpg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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