Crear una conexión TCP con certificados SSL mediante Node.js
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Abre una conexión TCP a Cloud SQL para PostgreSQL mediante el módulo Knex de Node.js con certificados SSL (capa de conexión segura).
Muestra de código
Salvo que se indique lo contrario, el contenido de esta página está sujeto a la licencia Atribución 4.0 de Creative Commons, y los ejemplos de código están sujetos a la licencia Apache 2.0. Para obtener más información, consulta las políticas del sitio de Google Developers. Java es una marca registrada de Oracle o sus afiliados.
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Información o código de muestra incorrectos","incorrectInformationOrSampleCode","thumb-down"],["Faltan la información o los ejemplos que necesito","missingTheInformationSamplesINeed","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Otro","otherDown","thumb-down"]],[],[],[],null,["# Create a TCP connection with SSL certificates by using Node.js\n\nOpen a TCP connection to Cloud SQL for PostgreSQL by using the Node.js knex module with SSL (Secure Sockets Layer) certificates.\n\nCode sample\n-----------\n\n### Node.js\n\n\nTo authenticate to Cloud SQL for PostgreSQL, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const Knex = require('knex');\n const fs = require('fs');\n\n // createTcpPool initializes a TCP connection pool for a Cloud SQL\n // instance of Postgres.\n const createTcpPool = async config =\u003e {\n // Note: Saving credentials in environment variables is convenient, but not\n // secure - consider a more secure solution such as\n // Cloud Secret Manager (https://cloud.google.com/secret-manager) to help\n // keep secrets safe.\n const dbConfig = {\n client: 'pg',\n connection: {\n host: process.env.INSTANCE_HOST, // e.g. '127.0.0.1'\n port: process.env.DB_PORT, // e.g. '5432'\n user: process.env.DB_USER, // e.g. 'my-user'\n password: process.env.DB_PASS, // e.g. 'my-user-password'\n database: process.env.DB_NAME, // e.g. 'my-database'\n },\n // ... Specify additional properties here.\n ...config,\n };\n\n // (OPTIONAL) Configure SSL certificates\n // For deployments that connect directly to a Cloud SQL instance without\n // using the Cloud SQL Proxy, configuring SSL certificates will ensure the\n // connection is encrypted.\n if (process.env.DB_ROOT_CERT) {\n dbConfig.connection.ssl = {\n rejectUnauthorized: false,\n ca: fs.readFileSync(process.env.DB_ROOT_CERT), // e.g., '/path/to/my/server-ca.pem'\n key: fs.readFileSync(process.env.DB_KEY), // e.g. '/path/to/my/client-key.pem'\n cert: fs.readFileSync(process.env.DB_CERT), // e.g. '/path/to/my/client-cert.pem'\n };\n }\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=cloud_sql_postgres)."]]