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

## URIを使う場合

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

```typescript
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を使わない場合
```typescript
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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/e97efa01-2900-67e1-891f-50e8a434f39d.png)

## URIを使う場合

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

```typescript
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を使わない場合

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

```typescript
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-----
MIIDxDCCAqygAwIBAgIUbLxMod62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U/d3MqBgBm5y+6
jB81TU/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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/c552c78c-3265-efb0-58fa-a1e72a242e0d.png)


![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/435735/ed64dfd4-df4c-727d-52a7-9593b3d6c9bc.png)


https://deno.land/x/postgres@v0.17.0