gpt4 book ai didi

node.js - 带有 node js pg 驱动程序的 CockroachDB

转载 作者:行者123 更新时间:2023-12-03 12:14:09 25 4
gpt4 key购买 nike

我正在尝试使用 pg 驱动程序连接 CockroachDB 和 Nodes JS。我能够成功建立连接,但是每次查询表时:它仅在我用数据库名称作为表名前缀时才有效,否则它会抛出关系不存在错误。虽然我在建立数据库连接时指定了数据库名称。
我用来建立数据库连接的代码:

    var pg = require('pg');
var config = {
user: 'root',
host: 'localhost',
database: 'testDB',
port: 26257
};
var pool = new pg.Pool(config);
const client = await pool.connect();
执行这一行工作正常,因为我用 DBname 前缀表名:
const response = await client.query('select * from testDB.test');
执行此行会引发以下错误:
const response = await client.query('select * from test');
(node:12797) UnhandledPromiseRejectionWarning: error: relation "test" does not exist
at Parser.parseErrorMessage (/Users/naveenkumar/Ucars/node-cockroachDB/node_modules/pg-protocol/dist/parser.js:278:15)
at Parser.handlePacket (/Users/naveenkumar/Ucars/node-cockroachDB/node_modules/pg-protocol/dist/parser.js:126:29)
at Parser.parse (/Users/naveenkumar/Ucars/node-cockroachDB/node_modules/pg-protocol/dist/parser.js:39:38)
at Socket.<anonymous> (/Users/naveenkumar/Ucars/node-cockroachDB/node_modules/pg-protocol/dist/index.js:10:42)
at Socket.emit (events.js:314:20)
at addChunk (_stream_readable.js:304:12)
at readableAddChunk (_stream_readable.js:280:9)
at Socket.Readable.push (_stream_readable.js:219:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23)
at TCP.callbackTrampoline (internal/async_hooks.js:123:14)
(Use `node --trace-warnings ...` to show where the warning was created)
<node_internals>/internal/process/warning.js:33
(node:12797) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
<node_internals>/internal/process/warning.js:33
(node:12797) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
任何形式的帮助表示赞赏,提前致谢:)

最佳答案

编辑:数据库名称应全部小写。见 https://www.cockroachlabs.com/docs/stable/keywords-and-identifiers.html#rules-for-identifiers

defaultdb> CREATE DATABASE TeSt;
CREATE DATABASE

Time: 166.382ms

defaultdb> SHOW DATABASES;
database_name | owner
-----------------------------+---------------------
defaultdb | root
test | lauren
testdb | lauren

使用 CockroachDB v20.2.2 和 pg v8.5.1,我无法重现该问题。以下按预期工作:
const { Pool } = require("pg");

const config = {
...
database: "testdb",
...
};

const pool = new Pool(config);
;(async function() {
const client = await pool.connect();
await client.query("select * from test_table", (err, res) => {
console.log(err, res);
client.end();
});
})()

关于node.js - 带有 node js pg 驱动程序的 CockroachDB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65248641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com