GDream Studio
GDream Studio4mo ago

Using SSL with deno deploy and supabase postgres direct connection

Hello 👋 I've been trying to setup an SSL connection with a supabase postgreSQL database using a direct connection. Unfortunaly it looks impossible to do it on Deno deploy. Am I right? I've been trying my best to figure out a solution but it looks like I need to either use --cert or the environment variables SSL_CERT_FILE and DENO_TLS_CA_STORE to make it work locally. However, on deno deploy, both options can't work: --cert isn't usable and DENO_TLS_CA_STORE is forbidden 😢 Any idea? I hope I don't need to abandon deno deploy. Thanks in advance!
5 Replies
lcasdev
lcasdev4mo ago
What database driver are you using? You can configure the CA certificate through the driver arguments usually
GDream Studio
GDream StudioOP4mo ago
I'm using postgresjs from deno.land/x and when I provide the CA certificate it doesn't work 😦 I have to use either --cert or SSL_CERT_FILE and DENO_TLS_CA_STORE. If I use npm:postgres it simply doesn't work.. no matter which one I use. @lucacasonato If you have a working example I'll take it 😅
lcasdev
lcasdev4mo ago
I don't know about npm:postgres, but I am relatively sure npm:pg should work Something like this:
// Import the Client from npm:pg
import { Client } from "npm:pg";

// Read the certificate file
const caCert = await Deno.readTextFile("./path/to/your/ca.crt");

const client = new Client({
user: "your_user",
password: "your_password",
database: "your_database",
hostname: "your_host",
port: 5432,
ssl: {
ca: caCert,
rejectUnauthorized: true, // Ensure the cert is validated
},
});

await client.connect();

const result = await client.queryObject("SELECT NOW()");
console.log(result.rows);

await client.end();
// Import the Client from npm:pg
import { Client } from "npm:pg";

// Read the certificate file
const caCert = await Deno.readTextFile("./path/to/your/ca.crt");

const client = new Client({
user: "your_user",
password: "your_password",
database: "your_database",
hostname: "your_host",
port: 5432,
ssl: {
ca: caCert,
rejectUnauthorized: true, // Ensure the cert is validated
},
});

await client.connect();

const result = await client.queryObject("SELECT NOW()");
console.log(result.rows);

await client.end();
GDream Studio
GDream StudioOP4mo ago
Thanks a lot 🙏 I'll give it a try and tell you if I made it work 😅 Indeed npm:pg is working 🥳 I'll have to figure out how to make it work with kysely now haha. Thanks a lot! All good for me!
lcasdev
lcasdev4mo ago
:partyparrot:

Did you find this page helpful?