4

I am trying to connect CockroachDB and Nodes JS using the pg driver. I am able to establish a connection successfully, but every time when querying the tables: it works only when I prefix the table names with the database name, or otherwise It throws relation doesn't exist error. Though I am specifying the database name while establishing DB connection.

The code that I am using to establish DB connection :

    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();

Executing this line works fine as I prefix table name with DBname:

const response = await client.query('select * from testDB.test');

Executing this line raises the following error:

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.

Any kind of help is appreciated, Thanks in advance :)

Mike Organek
  • 11,647
  • 3
  • 11
  • 26
naveen
  • 560
  • 6
  • 15

1 Answers1

4

EDIT: The database name should be all lowercase. See 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

With CockroachDB v20.2.2 and pg v8.5.1, I'm not able to reproduce the issue. The below works as expected:

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();
  });
})()  

lauren
  • 195
  • 4