Skip to main content

Python serverless Postgres driver for Neon — SQL over HTTP, sync and async (unofficial)

Project description

neon-serverless

Unofficial Python serverless Postgres driver for Neon — SQL over HTTP, sync and async. Neon ships an official driver for JavaScript/TypeScript (@neondatabase/serverless); this is a community Python counterpart, not affiliated with or endorsed by Neon, Inc.

Instead of opening a TCP connection and speaking the Postgres wire protocol, every query is a single HTTPS request to Neon's SQL-over-HTTP endpoint. That makes it a great fit for:

  • serverless functions (AWS Lambda, Vercel, Cloud Run, ...) where connections are short-lived
  • environments where outbound TCP on port 5432 is blocked
  • low-latency single-shot queries without connection setup cost

No libpq, no compiled extensions — the only dependency is httpx.

Install

pip install neon-serverless

Quickstart

import os
from neon_serverless import neon

sql = neon(os.environ["DATABASE_URL"])

rows = sql("SELECT * FROM posts WHERE author = $1", ["alice"])
# [{'id': 1, 'title': 'Hello', ...}, ...]

Async:

from neon_serverless import neon_async

sql = neon_async(os.environ["DATABASE_URL"])
rows = await sql("SELECT * FROM posts WHERE id = $1", [42])

Parameters

Use Postgres placeholders $1, $2, ... with a list of params. Supported Python types:

Python Sent as
None, bool, int, float, str JSON scalar
bytes bytea hex literal
datetime, date, time ISO string
Decimal string (exact)
UUID string
list / tuple Postgres array literal
dict JSON text

Result types

Values are decoded by Postgres type OID:

int2/int4/int8int (exact, no 2^53 limit), float4/float8float, numericDecimal, boolbool, byteabytes, json/jsonbdict/list, uuidUUID, date/time/timestamp → naive datetime objects, timestamptz/timetz → timezone-aware, arrays → list. Anything else (including interval) stays a str. Values that can't be parsed (e.g. infinity timestamps, BC dates) fall back to their raw string.

Options

sql = neon(
    conn_string,
    full_results=False,   # True: return FullQueryResults(command, row_count, rows, fields)
    array_mode=False,     # True: rows as lists instead of dicts
    auth_token=None,      # str or callable; sent as Authorization: Bearer (Neon RLS)
    endpoint=None,        # override SQL endpoint URL (default https://<host>/sql)
    timeout=30.0,
)

res = sql("SELECT 1 AS x", full_results=True)  # per-call override
res.command, res.row_count, res.rows, res.fields

Transactions

Multiple queries in one atomic HTTP request:

results = sql.transaction(
    [
        ("INSERT INTO logs (msg) VALUES ($1)", ["hi"]),
        "SELECT count(*) FROM logs",
    ],
    isolation_level="Serializable",  # ReadUncommitted | ReadCommitted | RepeatableRead | Serializable
    read_only=False,
    deferrable=False,
)

Queries run in a single transaction: all succeed or all roll back. Note there is no interactive BEGIN ... COMMIT — you must submit the whole batch up front (same constraint as the JS driver's transaction()).

Errors

Failed queries raise NeonDbError with the standard Postgres error fields:

from neon_serverless import NeonDbError

try:
    sql("SELECT * FROM missing")
except NeonDbError as e:
    print(e.code)      # 42P01
    print(e.severity)  # ERROR
    print(e.detail, e.hint, e.table, e.constraint, ...)

When NOT to use this

For long-running servers with steady traffic, a pooled TCP driver (psycopg or asyncpg) is faster per-query — Neon speaks standard Postgres too. This driver shines when connections are short-lived or TCP is unavailable.

Not yet supported (PRs welcome): WebSocket mode (full wire protocol with sessions, LISTEN/NOTIFY, cursors), COPY.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

neon_serverless-0.1.0.tar.gz (58.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

neon_serverless-0.1.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file neon_serverless-0.1.0.tar.gz.

File metadata

  • Download URL: neon_serverless-0.1.0.tar.gz
  • Upload date:
  • Size: 58.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for neon_serverless-0.1.0.tar.gz
Algorithm Hash digest
SHA256 936fb8725f3a10e99e8a0331df228e4e0e333f663c1bee561d5046b45dcc2f01
MD5 6596a065472c8e5134485aa5f3401a1e
BLAKE2b-256 5465297306a952bee13dcef67d57294ff8e7ca7ccf783da05cc0f89cef49bde2

See more details on using hashes here.

File details

Details for the file neon_serverless-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for neon_serverless-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 db02b04079565fe4f692b9198b19f79149814e44b19cdd8bd7cc5ac7f82d26b8
MD5 c63cf047e1fc7f275c8735c2dfdea4bf
BLAKE2b-256 03a27158b13982dcd192b5b4dbf2e04c4a20698160203688aaa2cce6df4f5300

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page