Connecting directly to Postgres
Supabase Edge Functions allow you to go beyond HTTP and can connect to your Postgres Database directly!
index.ts
_43import * as postgres from 'https://deno.land/x/postgres@v0.17.0/mod.ts'_43import { serve } from 'https://deno.land/std@0.177.0/http/server.ts'_43_43// Get the connection string from the environment variable "SUPABASE_DB_URL"_43const databaseUrl = Deno.env.get('SUPABASE_DB_URL')!_43_43// Create a database pool with three connections that are lazily established_43const pool = new postgres.Pool(databaseUrl, 3, true)_43_43serve(async (_req) => {_43 try {_43 // Grab a connection from the pool_43 const connection = await pool.connect()_43_43 try {_43 // Run a query_43 const result = await connection.queryObject`SELECT * FROM animals`_43 const animals = result.rows // [{ id: 1, name: "Lion" }, ...]_43 console.log(animals)_43_43 // Encode the result as pretty printed JSON_43 const body = JSON.stringify(_43 animals,_43 (key, value) => (typeof value === 'bigint' ? value.toString() : value),_43 2_43 )_43_43 // Return the response with the correct content type header_43 return new Response(body, {_43 status: 200,_43 headers: {_43 'Content-Type': 'application/json; charset=utf-8',_43 },_43 })_43 } finally {_43 // Release the connection back into the pool_43 connection.release()_43 }_43 } catch (err) {_43 console.error(err)_43 return new Response(String(err?.message ?? err), { status: 500 })_43 }_43})