by maest · 5y ago · 0 comments
import psycopg2
from psycopg2 import Error
try:
# Connect to an existing database
connection = psycopg2.connect(user="postgres",
password="pynative@#29",
host="127.0.0.1",
port="5432",
database="postgres_db")
cursor = connection.cursor()
cursor.execute("SELECT version();")
record = cursor.fetchone()
Having to manage the connection object myself is meh.I'd like to be able to do:
import pg_interface
pg = pg_interface.init(db_config)
pg.command(some_sql_string, some_params)
or even import pg_interface
pg_interface.command(some_sql_string, some_params, db_config)
and have the lib deal with initialising and managing any connection pools/objects. async with PostgrestClient("http://<yourhost>.com") as client:
r = await client.from_("countries").select("*").execute()
countries = r.json()
One advantage is that you don't have to worry about handling raw postgresql connections or pooling(as PostgREST does that for you), you only do HTTP requests. And if you want more complex SQL you can create functions or views.[1]: https://postgrest.org/en/v9.0/ecosystem.html#client-side-lib...