Skip to content
Dashboard

Postgres connections now work through Sandbox firewall

Software Engineer, Compute

Copy link to headingBackground

Copy link to headingWhat changed

Copy link to headingConnecting to hosted database

import { Sandbox } from '@vercel/sandbox';
const { PGHOST, PGUSER, PGPASSWORD, PGDATABASE } = process.env;
const connectionString = `postgres://${PGUSER}:${PGPASSWORD}@${PGHOST}:5432/${PGDATABASE}?sslmode=require`;
// Start with unrestricted network access to install dependencies.
const sandbox = await Sandbox.create();
await sandbox.runCommand({
cmd: 'sudo',
args: ['dnf', 'install', '-y', 'postgresql15'],
});
// Lock the sandbox down to only the database host before running untrusted code.
await sandbox.updateNetworkPolicy({
allowDomains: [PGHOST!],
});
const result = await sandbox.runCommand({
cmd: 'psql',
args: [connectionString, '-c', 'SELECT now();'],
});
console.log(await result.stdout());

Copy link to headingImportant to know

Ready to deploy?