简体   繁体   English

从我的 Node.js 后端查询我的 Cloud Sql DB

[英]Querying my Cloud Sql DB from my Node.js backend

Recently I've been learning google cloud sql it took a little but I was able to connect my cloud sql auth proxy to my postgres client.最近我一直在学习 google cloud sql 花了一点时间,但我能够将我的 cloud sql auth 代理连接到我的 postgres 客户端。 However I'm not sure how to query or make post request to my cloud sql.但是我不确定如何查询或向我的云 sql 发出发布请求。 Originally I was just doing本来我只是在做

const Pool = require("pg").Pool;

const pool = new Pool({
    user: "postgres",
    password: "****",
    host: "localhost",
    port: 5432,
    database: "somedb"
});

I'm not sure how to convert this over to try and query the cloud sql db.我不确定如何将其转换为尝试查询云 sql 数据库。 I did try converting it and got.我确实尝试过转换它并得到了。

const Pool = require("pg").Pool;

const pool = new Pool({
    user: "postgres",
    password: "****",
    host: "[cloud sql ip]",
    port: 5432,
    database: "[pg/gc db]"
});

I end up getting the error [pg_hba.conf rejects connection for host "[ipv4 ip]", user "postgres", database "[pg/gc db]", no encryption].我最终收到错误 [pg_hba.conf 拒绝主机“[ipv4 ip]”、用户“postgres”、数据库“[pg/gc db]”的连接,没有加密]。 I know that the documentation has a code sample but I don't understand it and cant really find any resources on explaining it.我知道文档有一个代码示例,但我不理解它,也找不到任何解释它的资源。

Edit: I am uploading files to a bucket in cloud storage which I was successfully able to do.编辑:我正在将文件上传到云存储中的存储桶,我成功地做到了。 I plan on mapping out all these files onto a webpage.我计划将所有这些文件映射到网页上。 However I would like to filter them by certain features so I am making a second request after I store the file.但是我想通过某些功能过滤它们,所以我在存储文件后发出第二个请求。 My second request will store attributes into a database that I can then relate to the files for filtering.我的第二个请求将属性存储到数据库中,然后我可以将其关联到文件以进行过滤。

If you're running the auth proxy from your local machine where you're running your application, then the code will be the same from your application's code perspective.如果您在运行应用程序的本地计算机上运行身份验证代理,那么从应用程序的代码角度来看,代码将是相同的。 You'll still connect to localhost (although you may need to connect to 127.0.0.1 depending on how you have hosts set up on the machine).您仍将连接到 localhost(尽管您可能需要连接到 127.0.0.1,具体取决于您在计算机上设置主机的方式)。

The database field will depend on how you've set up the database in Cloud SQL, but it should be the same as your local database.数据库字段取决于您在 Cloud SQL 中设置数据库的方式,但它应该与您的本地数据库相同。 Eg if you created a database named "somedb" in Cloud SQL, you don't have to change anything to connect to it in Cloud SQL.例如,如果您在 Cloud SQL 中创建了一个名为“somedb”的数据库,则无需更改任何内容即可在 Cloud SQL 中连接到它。 The proxy running locally will make everything behave as if you're running the database locally from the application's perspective.从应用程序的角度来看,本地运行的代理将使一切表现得就像您在本地运行数据库一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 云中的CSRF漏洞和我的Node.js后端的多个实例 - CSRF vulnerability & multiple instances of my Node.js backend in the cloud 仅从我的应用程序访问我的后端node.js - access my backend node.js only from my app Gremlin-从Node.js查询Titan db的最佳方法 - Gremlin - best approach for querying Titan db from Node.js 如何从不同项目中的 App Engine (Node.js) 连接到 Cloud SQL Postgres DB? - How to connect to Cloud SQL Postgres DB from App Engine (Node.js) in different project? 使用 Node.js 后端在我的 React 应用程序中启用 CORS - Enable CORS in my React App with Node.js backend 从Node.js Cloud Function插入Google Cloud SQL - Insert into Google Cloud SQL from Node.js Cloud Function 在 Windows 机器上的 node.js 上开发应用程序并使用后端 Oracle DB 在云上部署时出错 - Getting an Error when developing an App on node.js in windows machine and deploying on cloud with the backend Oracle DB 我的Node.js本地应用程序无法连接到Azure SQL DB - My Node.js local application won't connect to Azure SQL DB 为什么我从 node.js 后端下载的 csv 文件不包含 csv 数据? - Why does my csv file which I'm downloading from my node.js backend not contain csv data? 如何将我的客户端连接到云代工厂上的node.js服务器 - How to connect my client to a node.js server on cloud foundry
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM