Skip to content

Instantly share code, notes, and snippets.

@marcocastignoli
Created November 15, 2023 10:13
Show Gist options
  • Select an option

  • Save marcocastignoli/9ac2943bb1adcbb17e7403cc2bebcca7 to your computer and use it in GitHub Desktop.

Select an option

Save marcocastignoli/9ac2943bb1adcbb17e7403cc2bebcca7 to your computer and use it in GitHub Desktop.
// 1. Install `gcloud` cli
// 2. `gcloud init` to initialize gcloud with your account (it will open browser)
// 3. `gcloud auth application-default login` to set your account as the default account
import pg from "pg";
import { Connector } from "@google-cloud/cloud-sql-connector";
const { Pool } = pg;
const connector = new Connector();
const clientOpts = await connector.getOptions({
instanceConnectionName:
"",
authType: "IAM",
});
const pool = new Pool({
...clientOpts,
user: "",
database: "",
max: 5,
});
const { rows } = await pool.query(`
SELECT tablename
FROM pg_tables;
`);
console.table(rows);
await pool.end();
connector.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment