Does PostgreSQL Work With Cloudflare Pages?
PostgreSQL works with Cloudflare Pages through serverless functions or external APIs, but Pages itself cannot run a PostgreSQL instance—you need an external database provider.
Quick Facts
How PostgreSQL Works With Cloudflare Pages
Cloudflare Pages is a static site generator and edge-computing platform optimized for frontend deployment. It cannot host a PostgreSQL database directly, but you can absolutely use PostgreSQL as your data layer by connecting to it from Cloudflare Pages Functions (serverless edge functions) or from your backend API. The typical architecture involves Pages serving your frontend while Functions handle API requests that query PostgreSQL hosted on services like Vercel Postgres, AWS RDS, Railway, or any managed provider. Your Pages Functions act as the bridge—they authenticate with your database, execute queries, and return JSON responses to your frontend. This works smoothly for most applications, though you'll need to manage connection pooling carefully since edge functions are ephemeral and spawn frequently. For real-time features or heavy concurrent traffic, consider using PgBoss or similar job queues to offload background work from the function execution.
Best Use Cases
Cloudflare Pages Function querying PostgreSQL via node-postgres
npm install pgimport { Client } from 'pg';
export const onRequest: PagesFunction = async (context) => {
const client = new Client({
host: context.env.DB_HOST,
port: 5432,
user: context.env.DB_USER,
password: context.env.DB_PASSWORD,
database: context.env.DB_NAME,
ssl: true,
});
try {
await client.connect();
const result = await client.query('SELECT * FROM users LIMIT 10');
return new Response(JSON.stringify(result.rows), {
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
return new Response(`Error: ${error.message}`, { status: 500 });
} finally {
await client.end();
}
};Known Issues & Gotchas
Connection pooling: Each invoked Function creates a new connection, exhausting database connection limits quickly under high load
Fix: Use a connection pooler like PgBouncer, or leverage managed solutions like Vercel Postgres that handle pooling automatically. Alternatively, use serverless database proxies designed for edge functions.
Cold starts: Database queries from Functions can be slow on first execution due to connection initialization
Fix: Keep connection logic lightweight, use connection pooling, and consider caching frequently accessed data with Cloudflare KV or Cache API.
Network latency: Querying a geographically distant PostgreSQL instance from edge functions adds latency that negates some CDN benefits
Fix: Choose a database region close to your primary audience or use read replicas. Consider caching query results in Cloudflare KV.
Function execution timeout: Default 10-30 second timeout may not accommodate complex queries or large result sets
Fix: Optimize queries, paginate results, and use appropriate timeouts in your database driver configuration.
Alternatives
- •Vercel Functions + Vercel Postgres: Tighter integration with automatic connection pooling, but vendor lock-in
- •Supabase + Next.js (self-hosted on Pages): PostgreSQL-backed with built-in auth and real-time APIs, more opinionated
- •Railway/Render + Static Frontend (Pages): Manage your own backend server alongside Pages frontend, more control but more operational overhead
Resources
Related Compatibility Guides
Explore more compatibility guides