直接从Plain JS(浏览器)联系Postgres数据库

发布于 2025-02-13 16:49:36 字数 961 浏览 0 评论 0原文

假设我有一个远程Postgres数据库:

postgres://<user>:<passwd>@foo.us-west-1.compute.amazonaws.com:5432/<dbname>

连接到此数据库的常用方法是使用SQL库通过此请求来旋转后端服务器并发送请求。但是,我想知道是否可以使用普通JS直接从浏览器环境连接到DB,例如使用fetch

我尝试了几件事,但它们似乎都没有用。

  •   fetch(“ postgres://&lt; user&gt;:&lt; passwd&gt;@@@foo.us-west-west-west-west-compute.amazonaw.amazonaws.com:5432/&lt ; dbname&gt;“)
     
    给出URL方案“ Postgres”不支持
  •   fetch(“ http://foo.compute-1.amazonaws.com:5432/&lt = d.dbname>”)
     
    给出net :: err_empty_response
  •   fetch(“ https://foo.compute-1.amazonaws.com:5432/<
     
    返回net :: err_connection_closed

我想我需要将这些凭据放在我的请求中,但我不确定将它们放在哪里。

Let's say I have a remote Postgres database:

postgres://<user>:<passwd>@foo.us-west-1.compute.amazonaws.com:5432/<dbname>

The usual way to connect to this database would be to spin up a backend server and send requests through this using an SQL library. However, I'd like to know if it's possible to connect to the DB directly from a browser environment using plain JS, e.g. using fetch.

I tried a few things but none of them seem to work.

  • fetch("postgres://<user>:<passwd>@foo.us-west-1.compute.amazonaws.com:5432/<dbname>")
    

    gives URL scheme "postgres" is not supported.

  • fetch("http://foo.compute-1.amazonaws.com:5432/<dbname>")
    

    gives net::ERR_EMPTY_RESPONSE.

  • fetch("https://foo.compute-1.amazonaws.com:5432/<dbname>")
    

    returns net::ERR_CONNECTION_CLOSED.

I guess I need to put the credentials in my request somewhere, but I'm not sure where to put them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨凡馨 2025-02-20 16:49:41

提取在HTTP层上工作,Postgres本身不支持HTTP上的通信。因此,不可能直接从浏览器查询DB。

但是,有一些工具可以像 postgrest 在您的顶部创建HTTP接口, DB层,您可以用来与DB通信。

如果您使用的是AWS RDS等托管数据库,则可以使用其SDK从浏览器获取数据。

fetch works on HTTP layer and Postgres natively do not support communication on HTTP. So it is not possible to query your DB directly from the browser.

However, there are tools that you can configure like Postgrest which creates a HTTP interface on the top of your DB layer which you can use to communicate with DB.

If you are using a managed database like AWS RDS then you can use their SDKs to get data from the browser.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文