Integrating with Supabase Database (Postgres)
Connect to your Postgres database from Edge Functions.
Connect to your Postgres database from an Edge Function by using the supabase-js
client.
You can also use other Postgres clients like Deno Postgres
Using supabase-js
The supabase-js
client handles authorization with Row Level Security and automatically formats responses as JSON. This is the recommended approach for most applications:
123456789101112131415161718192021222324import { createClient } from 'npm:@supabase/supabase-js@2'Deno.serve(async (req) => { try { const supabase = createClient( Deno.env.get('SUPABASE_URL') ?? '', Deno.env.get('SUPABASE_ANON_KEY') ?? '', { global: { headers: { Authorization: req.headers.get('Authorization')! } } } ) const { data, error } = await supabase.from('countries').select('*') if (error) { throw error } return new Response(JSON.stringify({ data }), { headers: { 'Content-Type': 'application/json' }, status: 200, }) } catch (err) { return new Response(String(err?.message ?? err), { status: 500 }) }})
This enables:
- Automatic Row Level Security enforcement
- Built-in JSON serialization
- Consistent error handling
- TypeScript support for database schema
Using a Postgres client
Because Edge Functions are a server-side technology, it's safe to connect directly to your database using any popular Postgres client. This means you can run raw SQL from your Edge Functions.
Here is how you can connect to the database using Deno Postgres driver and run raw SQL. Check out the full example.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748import { Pool } from 'https://deno.land/x/postgres@v0.17.0/mod.ts'// Create a database pool with one connection.const pool = new Pool( { tls: { enabled: false }, database: 'postgres', hostname: Deno.env.get('DB_HOSTNAME'), user: Deno.env.get('DB_USER'), port: 6543, password: Deno.env.get('DB_PASSWORD'), }, 1)Deno.serve(async (_req) => { try { // Grab a connection from the pool const connection = await pool.connect() try { // Run a query const result = await connection.queryObject`SELECT * FROM animals` const animals = result.rows // [{ id: 1, name: "Lion" }, ...] // Encode the result as pretty printed JSON const body = JSON.stringify( animals, (_key, value) => (typeof value === 'bigint' ? value.toString() : value), 2 ) // Return the response with the correct content type header return new Response(body, { status: 200, headers: { 'Content-Type': 'application/json; charset=utf-8', }, }) } finally { // Release the connection back into the pool connection.release() } } catch (err) { console.error(err) return new Response(String(err?.message ?? err), { status: 500 }) }})
Using Drizzle
You can use Drizzle together with Postgres.js. Both can be loaded directly from npm:
Set up dependencies in import_map.json
:
1234567{ "imports": { "drizzle-orm": "npm:drizzle-orm@0.29.1", "drizzle-orm/": "npm:/drizzle-orm@0.29.1/", "postgres": "npm:postgres@3.4.3" }}
Use in your function:
1234567891011121314import { drizzle } from 'drizzle-orm/postgres-js'import postgres from 'postgres'import { countries } from '../_shared/schema.ts'const connectionString = Deno.env.get('SUPABASE_DB_URL')!Deno.serve(async (_req) => { // Disable prefetch as it is not supported for "Transaction" pool mode const client = postgres(connectionString, { prepare: false }) const db = drizzle(client) const allCountries = await db.select().from(countries) return Response.json(allCountries)})
You can find the full example on GitHub.
SSL connections
Production
Deployed edge functions are pre-configured to use SSL for connections to the Supabase database. You don't need to add any extra configurations.
Local development
If you want to use SSL connections during local development, follow these steps:
- Download the SSL certificate from Database settings
- Add to your local .env file, add these two variables:
12SSL_CERT_FILE=/path/to/cert.crt # set the path to the downloaded certDENO_TLS_CA_STORE=mozilla,system
Then, restart your local development server:
1supabase functions serve your-function