SQL Database
Each Invoke project can have a dedicated PostgreSQL database provisioned on demand. Once enabled, the connection is automatically injected into every function invocation — no configuration required.
Enabling the Database
- Open the Admin Panel and select your project.
- Go to SQL Database in the sidebar.
- Click Initialize Database.
Invoke creates:
- An admin user — full DDL + DML (CREATE, ALTER, DROP, INSERT, SELECT…)
- An app user — DML only (SELECT, INSERT, UPDATE, DELETE) — this is what your functions use
Your functions always run as the app user. You can manage your database via the SQL Database console or through a tunnel; both approaches use the admin user.
Connecting from a Function
No environment variables to set. When the database is initialized, Invoke automatically injects the connection details into the function's runtime environment. The PostgreSQL database connection is exposed via a Unix socket in /run/postgresql and /var/run/postgresql.
Below is the recommended way to connect to the database. You can always use your preferred framework or library.
- JavaScript
- TypeScript
- C#
Import sql from 'bun' — it's pre-configured with the injected DATABASE_URL. Use it directly as a tagged template literal, no setup needed.
import { sql } from 'bun'
export default async function handler(req, res) {
const rows = await sql`SELECT NOW() AS time`
res.json({ time: rows[0].time })
}
Import sql from 'bun' — it's pre-configured with the injected DATABASE_URL. Use it directly as a tagged template literal, no setup needed.
import { sql } from 'bun'
export default async function handler(req: InvokeRequest, res: InvokeResponse) {
const rows = await sql`SELECT NOW() AS time`
res.json({ time: rows[0].time })
}
Use Npgsql with the connection string provided by EnvironmentEx.GetConnectionString().
First, add Npgsql to your project:
dotnet add package Npgsql
using Invoke;
using Npgsql;
using System.Text.Json.Nodes;
public static class Function
{
[EntryPoint]
public static async Task EntryPoint(InvokeRequest req, InvokeResponse res)
{
await using var dataSource = NpgsqlDataSource.Create(EnvironmentEx.GetConnectionString());
await using var cmd = dataSource.CreateCommand("SELECT NOW() AS time");
var time = (DateTime)(await cmd.ExecuteScalarAsync())!;
res.Status(200).Json(new JsonObject { ["time"] = time.ToString("O") });
}
}
Creating Tables
Run schema migrations from the SQL Console in the Admin Panel, or from the CLI tunnel (see Connecting with CLI).
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
Basic Usage
To learn more about interacting with the SQL database, please refer to the following documentation.
Manage your Database
CLI Tunnel
Use the CLI to open a secure tunnel to your database for schema management, migrations, or direct inspection:
invoke sql:connect --project "Default Project"
# Use a custom local port (default: 5433)
invoke sql:connect --project "Default Project" --port 12345
Then connect with psql or any PostgreSQL client (e.g. pgAdmin 4, DataGrip):
psql -h localhost -p 5433
The tunnel forwards to your project's database with admin user over the secure WebSocket connection.
See sql:connect in the CLI Reference for the full option list.
SQL Console
The Admin Panel → SQL Database → Console tab lets you run queries directly against your database without leaving the browser. It supports:
- Multi-statement queries
- Session SQL (persistent
SETcommands per connection) - Query history (last 50 queries)
SQL Console always connect with admin user to your project's database.
Storage Quota
Each project has a storage quota displayed in the SQL Database page. If you approach the limit, INSERT/UPDATE queries will return a storage warning and execution will be denied. Contact your administrator to increase the quota or use DELETE or TRUNCATE to free up storage.