Connect Postgres
This tutorial connects the api Resource to a Neon
Postgres database over Neon’s HTTPS serverless driver. You store the connection
string as a secret, allowlist the database hostname for egress, and run a query
from your code.
Time: about ten minutes. You need the hyjal CLI, a
logged-in session, and a Neon project with a connection string.
Why the HTTP driver
Instances have no ambient network access and hold no TCP connections. Neon’s serverless driver speaks HTTPS, so it works from any lane once the hostname is on the Resource’s egress allowlist. This is the recommended default for databases on Hyjal. For classic TCP databases, use the Database Door instead, covered at the end of this page.
1. Get the connection string
In the Neon console, copy the pooled connection string. It looks like:
Note the hostname: ep-cool-block-123.us-east-2.aws.neon.tech. You allowlist
that exact host in step 3.
2. Store the connection string as a secret
Set the connection string as a Project secret. Secrets are injected into the
instance environment per request and never live in hyjal.toml or in git.
Confirm it is stored (values are never printed back):
3. Allowlist the database host and reference the secret
In hyjal.toml, add the Neon hostname to the api
Resource’s egress allowlist and reference the secret with @secret.
The @secret marker tells the platform to resolve DATABASE_URL from the
secret store at request time. Outbound HTTPS is permitted only to hosts on the
egress list; every other destination is refused with
HYJ-NET001.
4. Query from your code
Use Neon’s serverless driver, reading DATABASE_URL from the environment.
5. Deploy and run the first query
Call the endpoint. The response carries a timestamp straight from Postgres:
If the query fails with an egress error, confirm the allowlisted hostname exactly matches the host in your connection string.
Using the Database Door instead
If your database only speaks the raw Postgres TCP protocol, route through the Database Door. The platform holds pooled, TLS-verified connections host-side; your code sends parameterized queries over HTTP and gets JSON rows back. Credentials are stored as Project secrets, and instances never hold a raw TCP connection: the pooling is the platform’s job, which keeps instance-per-request safe against connection storms.
What you built
- A Neon Postgres connection string stored as a Project secret.
- An egress allowlist scoped to the database hostname.
- A deployed
apiResource that runs a live query per request.
Next steps
- Store state without an external database: Hyjal Tables tutorial
- Read the data model: Data overview
- Route TCP databases through the Door: Database Door