直接从Plain JS(浏览器)联系Postgres数据库
假设我有一个远程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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
提取
在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.