Skip to content

Commit

Permalink
Example showing prepared statements with pg8000 (#821)
Browse files Browse the repository at this point in the history
Example showing prepared statements with pg8000
  • Loading branch information
hlcianfagna authored Jan 31, 2025
1 parent 8c31d35 commit fcc4237
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
41 changes: 41 additions & 0 deletions by-language/python-pg8000/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.. highlight: console
#################################################
Connect to CrateDB and CrateDB Cloud using pg8000
#################################################


*****
About
*****

Example programs demonstrating CrateDB with pg8000,
for the PostgreSQL protocol.


*****
Setup
*****

To start a CrateDB instance on your machine for evaluation purposes, invoke::

docker run --publish 4200:4200 --publish 5432:5432 --env CRATE_HEAP_SIZE=1g crate:latest -Cdiscovery.type=single-node

Navigate to the example program directory, and install prerequisites::

# Acquire sources.
git clone https://github.com/crate/cratedb-examples
cd cratedb-examples
python3 -m venv .venv
source .venv/bin/activate
pip install pg8000


********
Examples
********

Run an example program::

python basic.py

26 changes: 26 additions & 0 deletions by-language/python-pg8000/basic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import ssl

import pg8000

# When connecting to CrateDB Cloud you may want to use SSL
# ssl_context = ssl.create_default_context()

conn = pg8000.connect(
user="crate",
password="",
host="localhost",
port=5432,
database="doc",
# ssl_context=ssl_context,
)

query = """
SELECT mountain,height
FROM sys.summits
WHERE height >= :minimum_height
ORDER BY height DESC
LIMIT :number_of_rows;
"""

ps = conn.prepare(query)
ps.run(minimum_height=4000, number_of_rows=10)

0 comments on commit fcc4237

Please sign in to comment.