Databases
Managed Postgres with auto-provisioning and credentials API
Overview
Every project can have managed Postgres databases. The SDK provides methods to list, create, and retrieve credentials for databases. The ensure method makes database provisioning idempotent — call it on every deploy and it will only create the database if it does not already exist.
All database methods are available on r.databases.
Methods
List databases
Parameters:
Returns: SingleResponse<Database[]>
Create a database
Input fields:
Ensure a database (idempotent)
The ensure method creates the database if it does not exist, or returns the existing one. This is the recommended approach for deployment scripts and CI/CD pipelines.
Input fields:
ensure is idempotent. Calling it multiple times with the same project and name will always return the same database without creating duplicates.
Get credentials
Retrieve the connection credentials for a database. Credentials are returned as individual fields and a ready-to-use connection string.
Parameters:
getCredentials takes project_id and name — not a database ID. This is intentional: the database name is the stable identifier you use in your code.
Returns: SingleResponse<DatabaseCredentials>
Query a database
Execute SQL queries directly through the SDK — no need to manage database connections yourself.
Parameters:
Returns: SingleResponse<QueryResult>
Always use parameterized queries ($1, $2) instead of string interpolation to prevent SQL injection.
Full example: provision and migrate
This example shows a complete workflow: ensure a database exists, get credentials, and run a migration via sandbox code execution.
Using credentials with ORMs
The credentials object works directly with popular Node.js database libraries: