Edits history of script submission #22493 for ' Postgres with TLS / SSL (postgresql)'

  • bun
    One script reply has been approved by the moderators
    Ap­pro­ved
    import { Client } from "pg";
    
    type Postgresql = {
      host: string;
      port: number;
      user: string;
      dbname: string;
      sslmode: string;
      password: string;
    };
    
    type Cacertificate = {
      certificate: string;
    };
    export async function main(
      dbConfig: Postgresql,
      cacertificate: Cacertificate,
      sql: string = "SELECT 1 as id",
    ) {
      const client = new Client({
        host: dbConfig.host,
        port: dbConfig.port,
        user: dbConfig.user,
        database: dbConfig.dbname,
        password: dbConfig.password,
        ssl: {
          rejectUnauthorized: true,
          ca: cacertificate.certificate,
        },
      });
      await client.connect();
    
      const res = await client.query(sql);
    
      await client.end();
    
      return res.rows;
    }
    

    Submitted by hugo989 4 days ago