Created
November 15, 2023 10:13
-
-
Save marcocastignoli/9ac2943bb1adcbb17e7403cc2bebcca7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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