3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Deno DeployからSupabase(PostgreSQL)に接続する

Last updated at Posted at 2023-04-22

URIを使う場合

パスワードは、encodeURIComponent('YOUR_PASSWORD_HERE')を通すこと

import { serve } from "https://deno.land/std@0.177.0/http/server.ts";
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";

const client = new Client('postgresql://postgres:YOUR_PASSWORD_HERE@db.gnrghrfzxujxijnkdssb.supabase.co:5432/postgres')
await client.connect();

serve(async (req: Request) => {
    const result = await client.queryObject("SELECT * FROM item")
    return new Response(JSON.stringify(result, null, 2))
});

URIを使わない場合

import { serve } from "https://deno.land/std@0.177.0/http/server.ts";
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";

const client = new Client({
  user: "postgres",
  password: "YOUR_PASSWORD_HERE",
  database: "postgres",
  hostname: "db.gnrghrfzxujxijnkdssb.supabase.co"
});
await client.connect();

serve(async (req: Request) => {
    const result = await client.queryObject("SELECT * FROM item")
    return new Response(JSON.stringify(result, null, 2))
});

TLS connection failed with message: invalid peer certificate contents: invalid peer certificate: UnknownIssuer

image.png

URIを使う場合

パスワードは、encodeURIComponent('YOUR_PASSWORD_HERE')を通すこと

import { serve } from "https://deno.land/std@0.177.0/http/server.ts";
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";

const client = new Client('postgresql://postgres:YOUR_PASSWORD_HERE@db.gnrghrfzxujxijnkdssb.supabase.co:5432/postgres?sslmode=disable')
await client.connect();

serve(async (req: Request) => {
    const result = await client.queryObject("SELECT * FROM item")
    return new Response(JSON.stringify(result, null, 2))
});

URIを使わない場合

なんかエラーが出る場合。証明書はダウンロードしてね

import { serve } from "https://deno.land/std@0.177.0/http/server.ts";
import { Client } from "https://deno.land/x/postgres@v0.17.0/mod.ts";

const cert = `-----BEGIN CERTIFICATE-----
MIIDxDCCAqygAwIBAgIUbLxMod62P2ktCiAkxnKJwtE9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B81TU/RG2rVerPDWP+1MMcNNy0491CTL5XQZ7JfDJJ9CCmXSdtTl4uUQnSuv/Qx
Cea13BX2ZgJc7Au30vihLhub52De4P/4gonKsNHYdbWjg7OWKwNv/zitGDVDB9Y2
CMTyZKG3XEu5Ghl1LEnI3QmEKsqaCLv12BnVjbkSeZsMnevJPs1Ye6TjjJwdik5P
o/bKiIz+Fq8=
-----END CERTIFICATE-----`

const client = new Client({
  user: "postgres",
  password: "YOUR_PASSWORD_HERE",
  database: "postgres",
  hostname: "db.gnrghrfzxujxijnkdssb.supabase.co",
  tls: { caCertificates: [cert] },
});
await client.connect();

serve(async (req: Request) => {
    const result = await client.queryObject("SELECT * FROM item")
    return new Response(JSON.stringify(result, null, 2))
});

image.png

image.png

3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?