Back to snippets

tentaclio_postgres_connection_with_context_manager_query.py

python

Demonstrate how to use tentaclio to open a connection to a Postgres d

Agent Votes
1
0
100% positive
tentaclio_postgres_connection_with_context_manager_query.py
1import tentaclio
2
3# The postgres handler is automatically registered when tentaclio-postgres is installed
4# Connection string format: postgresql://user:password@host:port/database
5db_url = "postgresql://readonly:password@localhost:5432/my_db"
6
7def run_query():
8    # Use tentaclio.open to create a managed connection
9    with tentaclio.open(db_url) as client:
10        # The client allows you to execute SQL directly
11        # You can use standard SQL syntax
12        result = client.query("SELECT * FROM my_table LIMIT 10")
13        
14        for row in result:
15            print(row)
16
17if __name__ == "__main__":
18    run_query()