feat: POSTGRES example — add pg-table-reader HTTP function
This commit is contained in:
@@ -0,0 +1 @@
|
||||
psycopg2-binary==2.9.9
|
||||
@@ -0,0 +1,27 @@
|
||||
# 2026-03-18
|
||||
# table_reader.py — HTTP-функция: читает строки из terraform_demo_table и возвращает JSON.
|
||||
# Подключается к Postgres через env vars (те же что у sql-runner).
|
||||
import os
|
||||
import psycopg2
|
||||
import psycopg2.extras
|
||||
|
||||
|
||||
def list_rows(event):
|
||||
# Возвращает все строки terraform_demo_table в порядке убывания created_at.
|
||||
connection = psycopg2.connect(
|
||||
host=os.environ["PGHOST"],
|
||||
port=os.environ.get("PGPORT", "5432"),
|
||||
dbname=os.environ["PGDATABASE"],
|
||||
user=os.environ["PGUSER"],
|
||||
password=os.environ["PGPASSWORD"],
|
||||
sslmode=os.environ.get("PGSSLMODE", "require"),
|
||||
)
|
||||
try:
|
||||
cursor = connection.cursor(cursor_factory=psycopg2.extras.RealDictCursor)
|
||||
cursor.execute(
|
||||
"SELECT id, title, created_at::text FROM terraform_demo_table ORDER BY created_at DESC"
|
||||
)
|
||||
rows = [dict(row) for row in cursor.fetchall()]
|
||||
return {"rows": rows, "count": len(rows)}
|
||||
finally:
|
||||
connection.close()
|
||||
Reference in New Issue
Block a user