Skip to content
Dashboard

Postgres connections now work through Sandbox firewall

Software Engineer, Compute

Link to headingBackground

Link to headingWhat changed

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());

Link to headingImportant to know

Ready to deploy?