Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: add edge functions postgres exmple.

thorwebdev8cd09dbfc2d2
diff --git a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
index bbf800c33b0..1f6336105df 100644
--- a/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
+++ b/apps/docs/components/Navigation/NavigationMenu/NavigationMenu.constants.ts
@@ -453,6 +453,11 @@ export const functions = {
url: '/guides/functions/examples/cloudflare-turnstile',
items: [],
},
+ {
+ name: 'Connect to Postgres',
+ url: '/guides/functions/examples/connect-to-postgres',
+ items: [],
+ },
{ name: 'GitHub Actions', url: '/guides/functions/examples/github-actions', items: [] },
{ name: 'OG Image', url: '/guides/functions/examples/og-image', items: [] },
{ name: 'Storage Caching', url: '/guides/functions/examples/storage-caching', items: [] },
@@ -816,7 +821,6 @@ export const reference_dart_v1 = {
parent: '/reference',
}
-
export const reference_csharp_v0 = {
icon: 'reference-csharp',
title: 'c#',
diff --git a/apps/docs/pages/guides/functions/examples/connect-to-postgres.mdx b/apps/docs/pages/guides/functions/examples/connect-to-postgres.mdx
new file mode 100644
index 00000000000..3b66a4affd9
--- /dev/null
+++ b/apps/docs/pages/guides/functions/examples/connect-to-postgres.mdx
@@ -0,0 +1,68 @@
+import Layout from '~/layouts/DefaultGuideLayout'
+
+export const meta = {
+ id: 'examples-postgres-on-the-edge',
+ title: 'Connect to Postgres',
+ description: 'Connecting to Postgres from Edge Functions.',
+}
+
+<div class="video-container">
+ <iframe
+ src="https://www.youtube-nocookie.com/embed/cl7EuF1-RsY"
+ frameBorder="1"
+ allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture"
+ allowFullScreen
+ ></iframe>
+</div>
+
+Supabase Edge Functions allow you to go beyond HTTP and can connect to your Postgres Database directly!
+
+```ts index.ts
+import * as postgres from 'https://deno.land/x/[email protected]/mod.ts'
+import { serve } from 'https://deno.land/[email protected]/http/server.ts'
+
+// Get the connection string from the environment variable "DATABASE_URL"
+const databaseUrl = Deno.env.get('DATABASE_URL')!
+
+// Create a database pool with three connections that are lazily established
+const pool = new postgres.Pool(databaseUrl, 3, true)
+
+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" }, ...]
+ console.log(animals)
+
+ // 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 })
+ }
+})
+```
+
+export const Page = ({ children }) => <Layout meta={meta} children={children} />
+
+export default Page