是否有可能将 postgreSQL 直接连接到 Javascript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5029358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 15:19:27  来源:igfitidea点击:

Is there a possibility of connecting postgreSQL directly to Javascript?

javascriptpostgresql

提问by Benz639

without having to use php, python or odbc?

无需使用 php、python 或 odbc?

回答by Quentin

You can get a JS driver for Postgres from https://github.com/creationix/postgres-js

您可以从https://github.com/creationix/postgres-js获取 Postgres 的 JS 驱动程序

This one is designed for use with node.js. Don't expect to be able to find something you can run client side in a web browser.

这个是专为与 node.js 一起使用而设计的。不要期望能够找到可以在 Web 浏览器中运行客户端的东西。

回答by Thariama

No, keep in mind that Javascript works client side only when used in a browser while a database can only be connected from server side. Thus you will need to call a serverside script in PHP, Python or another server-side language in order to get to the results.

不,请记住 Javascript 仅在浏览器中使用时才在客户端工作,而数据库只能从服务器端连接。因此,您需要使用 PHP、Python 或其他服务器端语言调用服务器端脚本才能获得结果。

回答by mapsa

I have used Postgrest (postgrest.com).

我使用过 Postgrest (postgrest.com)。

"PostgREST is a standalone web server that turns your PostgreSQL database directly into a RESTful API."

“PostgREST 是一个独立的 Web 服务器,可将您的 PostgreSQL 数据库直接转换为 RESTful API。”

Then you can make a query with a url which returns data in json format.

然后,您可以使用以 json 格式返回数据的 url 进行查询。

回答by gor

Yes, it is possible if your javascript runs on node.js. Here is connector.

是的,如果您的 javascript 在 node.js 上运行是可能的。这是连接器

回答by Jasurbek Nabijonov

It is possible. Please see following code. Before using it, you should update Node.jsto 7.6.0 or higher. You can use Postgresqlby calling only main(yourQuery)function. Found it on Google.

有可能的。请看下面的代码。在使用它之前,您应该更新Node.js到 7.6.0 或更高版本。您可以Postgresql通过仅调用main(yourQuery)函数来使用。在谷歌上找到它。

const pg = require('pg')

// create a config to configure both pooling behavior
// and client options
// note: all config is optional and the environment variables
// will be read if the config is not present
var config = {
    user: 'username', // env var: PGUSER
    database: 'databaseName', // env var: PGDATABASE
    password: 'Password', // env var: PGPASSWORD
    host: 'localhost', // Server hosting the postgres database
    port: 35432, // env var: PGPORT
    max: 10, // max number of clients in the pool
    idleTimeoutMillis: 30000 // how long a client is allowed to remain idle before being closed
}


const pool = new pg.Pool(config)

async function query (q) {
    const client = await pool.connect()
    let res
    try {
        await client.query('BEGIN')
        try {
            res = await client.query(q)
            await client.query('COMMIT')
        } catch (err) {
            await client.query('ROLLBACK')
            throw err
        }
    } finally {
        client.release()
    }
    return res
}

async function main (queryStr) {
    try {
        const { rows } = await query(queryStr);
        console.log(JSON.stringify(rows));
    } catch (err) {
        console.log('Database ' + err)
    }
}
main('SELECT * FROM user where user = \'123\'') 

回答by Dean

I never worked with PostgreSQL, but as far as I know Databases require a valid credentials (username and password) to access them. With JavaScript you have no way of hiding the username and password, as the script is sent to the client. So theoretically if you could do that, any client would be able to run queries, and do whatever they want with your database.

我从未使用过 PostgreSQL,但据我所知,数据库需要有效的凭据(用户名和密码)才能访问它们。使用 JavaScript,您无法隐藏用户名和密码,因为脚本已发送到客户端。所以理论上,如果你能做到这一点,任何客户端都可以运行查询,并对你的数据库做任何他们想做的事情。

Anyways, you cannot access a database from the client side.

无论如何,您无法从客户端访问数据库。

回答by TNC

Nope. Javascript is client-side only. You need some sort of server-side language/interface.

不。Javascript 只是客户端。您需要某种服务器端语言/界面。