Use a managed Postgres database from an app hosted anywhere
You can run a managed Postgres database on thelastapi and connect to it from any server, container, or platform using a standard DATABASE_URL.
On this page 8 sections
You can use a managed Postgres database from an app hosted anywhere by providing standard connection strings or loading your environment with a single application key. thelastapi provisions standard Postgres accessible over the internet, so any language, ORM, or migration tool connects directly. You do not need to host your web application on our infrastructure to use the database.
Whether your application runs in a container, on a virtual private server, on serverless functions, or as a background worker, setup takes a single command. Here is how to create a database, connect your application from any host, handle local development safely, and manage migrations.
How to create a standalone database
When you do not need our hosting, create a project containing only the database resource:
vibiz init my-backend --resources databaseThis creates the project and provisions a managed serverless Postgres database. If you already have an existing project, you can add Postgres at any time:
vibiz add databaseProjects on thelastapi are modular. You can use Postgres by itself, or alongside sign-in, payments, email, or AI models. None of these require you to use our hosting. For a broader overview of running standalone services, see how to use just one service from an app hosted anywhere.
Usage records each ready database at an estimated $0.02 a day of cost, billed with the default margin (+20% today, tracked as billed_usd in usage reports).
How to get your connection details to any host
You have two ways to supply your application with its database connection string on an external host:
Option 1: Load environment variables at boot with an app key
Instead of manually copying database passwords across dashboards, you can place a single environment variable—VIBIZ_APP_KEY—on your hosting provider.
At server startup, import @vibiz/app and call loadEnv(). This pulls your production DATABASE_URL directly into process.env:
import { vibiz } from '@vibiz/app';
// Injects DATABASE_URL and other project variables into process.env
await vibiz.loadEnv();
import postgres from 'postgres';
const sql = postgres(process.env.DATABASE_URL!);
const rows = await sql`SELECT id, email FROM users LIMIT 10`;
console.log(rows);If your application is written in Go, Python, or Ruby, or if you prefer not to use the JavaScript SDK, you can inject variables at startup using curl:
eval "$(curl -fsS -H "Authorization: Bearer $VIBIZ_APP_KEY" \
https://thelastapi.dev/v1/app/env?format=shell)" && exec node server.jsYou can also fetch your environment variables formatted for different runtimes via GET /v1/app/env?format=json|dotenv|shell|docker. During your build step or CI pipeline, you can write them out to a file:
npx @vibiz/cli env pull --format dotenv -o .env.productionOr wrap your startup command directly:
npx @vibiz/cli run -- npm startOption 2: Extract the production DATABASE_URL directly
If you prefer to paste your connection string directly into your hosting dashboard, export the production environment variables using a full-access key:
vibiz envTo learn more about retrieving credentials via the terminal, read how to get a Postgres DATABASE_URL in one command.
How local development and migrations work
Connecting directly to a live database during development leads to accidental data loss. thelastapi isolates local development by providing an isolated development branch for every project.
Run this command in your project directory:
vibiz devvibiz dev provisions a development database copy. This copy duplicates production's schemas, tables, and migration history, but contains none of production's rows. Every table is emptied before the branch is handed to your laptop or coding agent.
The command writes the development connection string to .env.development.local:
# Generated by vibiz dev
DATABASE_URL="postgres://..."Because migration ledgers (such as those used by Drizzle or Prisma) are preserved during branching, migration runners know which migrations have already applied. You run your normal tools directly against the development branch:
# Run migrations using your preferred tools
npx drizzle-kit migrate
# or
npx prisma migrate devFor detailed setup steps with specific frameworks, see how to use Postgres from Next.js with Drizzle or Prisma and our guide on serverless Postgres for side projects with one database per project.
If your local database gets into a messy state, reset it with:
vibiz dev --resetThe next vibiz dev command branches fresh from your production schema.
How permissions protect production rows
When working with team members, CI runners, or AI coding agents, access control prevents unauthorized reads of production user data. Keys exist in two access tiers: Full-access and Limited.
| Action | Full-access key (Owner) | Limited key (Agent / CI) |
|---|---|---|
Read production DATABASE_URL | Yes (vibiz env) | No |
| Connect to development branch | Yes (vibiz dev) | Yes (vibiz dev) |
| Read-only SQL via console or API | Yes | Development branch only |
| Direct writes via Postgres protocol | Yes | Development branch only |
| Export production database dump | Yes | No (403 owner_only) |
If a Limited key attempts to read production user data via the API, the gateway responds with an error:
{
"error": {
"code": "forbidden",
"status": 403,
"details": {
"reason": "owner_only",
"permission": "database.production:read",
"grant_url": "https://thelastapi.dev/keys/key_123/allow?permission=database.production:read"
}
}
}The account owner can selectively grant a Limited key permission to read production data without granting full account ownership:
vibiz keys access key_123 allow database.production:readRead more about configuring environment tiers and permissions in the keys documentation.
Inspecting your database without a local client
You do not need to install psql or a local GUI to inspect schemas or run validation queries. thelastapi provides HTTP endpoints for read-only SQL queries.
To list tables and estimated row counts:
curl "https://thelastapi.dev/v1/projects/prj_123/database?env=development" \
-H "Authorization: Bearer $VIBIZ_API_KEY"To execute a single read-only SQL statement:
curl https://thelastapi.dev/v1/projects/prj_123/database/query \
-H "Authorization: Bearer $VIBIZ_API_KEY" \
-H "Content-Type: application/json" \
-d '{"sql":"SELECT table_name FROM information_schema.tables WHERE table_schema = '\''public'\''","env":"development"}'The read-only query endpoint has specific guardrails:
- Statements must start with
SELECT,WITH,TABLE,VALUES,EXPLAIN, orSHOW. - Query text can be up to 20,000 characters.
- Query execution timeout is 5 seconds.
- Results return up to the first 500 rows (flagging
"truncated": trueif more exist). Table browsing views return up to 200 rows.
Statements that attempt to modify data return a 400 invalid_request error. To write data, connect using standard database client libraries and your connection URL. Read more in the database documentation.
Exporting and taking your data elsewhere
There is no proprietary lock-in. You can download an exact dump of your Postgres database at any time.
Generate an export package containing your schema, tables, rows, keys, indexes, views, and functions:
vibiz export --dbTo export table data as CSV files in addition to the SQL dump:
vibiz export --db --csvThe generated .sql.gz file contains clean Postgres statements stripped of specific owner roles or grants. To restore your database onto any Postgres 15+ instance running on another provider:
gunzip -c my-backend-production-database.sql.gz | psql "$NEW_DATABASE_URL"Exports also generate a VIBIZ-EXPORT.md architecture guide and an .env.example mapping out all variables your application uses. Detailed operational details are covered in the export documentation and service decoupling documentation.
Next step
Initialize a project with a managed database by running vibiz init <project-name> --resources database. Then run vibiz dev to generate your development connection string and start building.
Frequently asked questions
Can I connect to the database from a serverless function hosted outside thelastapi?
Yes. thelastapi provides a standard Postgres connection string that is reachable over the public internet, so serverless functions, background workers, and long-running containers on any provider can connect directly.
How do migrations stay in sync between my development branch and production?
Migration ledgers such as Prisma's and Drizzle's are preserved when `vibiz dev` creates your development branch, ensuring existing migrations are recognized and only new migrations execute when you test locally.
What happens if an AI coding agent or CI runner tries to access production database rows?
Limited keys assigned to agents or CI receive a `403 owner_only` error when attempting to query production data; they can only query the empty development branch unless the owner explicitly grants `database.production:read`.
How much does a standalone database cost on thelastapi?
Each ready database records an estimated baseline cost of $0.02 per day, billed with the default margin (+20% today, tracked as `billed_usd` in usage records).