error: too many connections for role "databaseName" - node.js

I am using typeorm datasource to connect to elephantsql postgres, been getting this issue, can anyone help with the fix
error: too many connections for role "databaseName"
at Parser.parseErrorMessage (node_modules/pg-protocol/src/parser.ts:369:69)
at Parser.handlePacket (node_modules/pg-protocol/src/parser.ts:188:21)
at Parser.parse (node_modules/pg-protocol/src/parser.ts:103:30)
at Socket.<anonymous> (node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Socket.Readable.push (node:internal/streams/readable:234:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
length: 110,
severity: 'FATAL',
code: '53300',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'miscinit.c',
line: '661',
routine: 'InitializeSessionUserId'
}

Free ElephantSQL databases accept only 1 connection. Try changing the pool setting extra.max to 1:
export const AppDataSource = new DataSource({
// ...postgresConfig
extra: {
max: 1, // set pool max size
}
});

Related

PG Pool: relation does not exist

I recently started coding in javascript. Been trying to use pg module to interact with my postgres db. But it keeps saying relation does not exist even though everything is okay and the table does exist. Any help will be appreciated.
const Pool = require('pg').Pool
const pool = new Pool({
user: 'postgres',
host: 'localhost',
database: 'test',
password: 'test#123',
port: '5432',
})
pool.query("SELECT * FROM clients", (error, results) => {
if (error) throw error
console.log(results)
})
module.exports = pool```
Error:
```error: relation "clients" does not exist
at Parser.parseErrorMessage (C:\Users\Maaz\Desktop\Code\acube_backend\node_modules\pg-protocol\dist\parser.js:287:98)
at Parser.handlePacket (C:\Users\Maaz\Desktop\Code\acube_backend\node_modules\pg-protocol\dist\parser.js:126:29)
at Parser.parse (C:\Users\Maaz\Desktop\Code\acube_backend\node_modules\pg-protocol\dist\parser.js:39:38)
at Socket.<anonymous> (C:\Users\Maaz\Desktop\Code\acube_backend\node_modules\pg-protocol\dist\index.js:11:42)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
length: 106,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: '15',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_relation.c',
line: '1384',
routine: 'parserOpenTable'
}

How do I use Sequelize to connect to Heroku Postgres?

this is my configuration:
import { Sequelize } from "sequelize";
import dotenv from 'dotenv';
dotenv.config();
export const sequelize = new Sequelize(process.env.DATABASE_URL as string,
{
dialect: 'postgres',
}
)
error:
ConnectionError [SequelizeConnectionError]: no pg_hba.conf entry for host "186.194.175.39", user "xuavcbfkstvqrm", database "d3t76co8ov8vqm", SSL off
at Client._connectionCallback (d:\Projetos-VSCODE\Crud-tsnode\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:189:24)
at Client._handleErrorWhileConnecting (d:\Projetos-VSCODE\Crud-tsnode\node_modules\pg\lib\client.js:305:19)
at Client._handleErrorMessage (d:\Projetos-VSCODE\Crud-tsnode\node_modules\pg\lib\client.js:325:19)
at Connection.emit (node:events:390:28)
at Connection.emit (node:domain:475:12)
at d:\Projetos-VSCODE\Crud-tsnode\node_modules\pg\lib\connection.js:114:12
at Parser.parse (d:\Projetos-VSCODE\Crud-tsnode\node_modules\pg-protocol\src\parser.ts:104:9)
at Socket.<anonymous> (d:\Projetos-VSCODE\Crud-tsnode\node_modules\pg-protocol\src\index.ts:7:48)
at Socket.emit (node:events:390:28)
at Socket.emit (node:domain:475:12) {
parent: error: no pg_hba.conf entry for host "186.194.175.39", user "xuavcbfkstvqrm", database "d3t76co8ov8vqm", SSL off
at Parser.parseErrorMessage (d:\Projetos-VSCODE\Crud-tsnode\node_modules\pg-protocol\src\parser.ts:369:69)
at Parser.handlePacket (d:\Projetos-VSCODE\Crud-tsnode\node_modules\pg-protocol\src\parser.ts:188:21)
at Parser.parse (d:\Projetos-VSCODE\Crud-tsnode\node_modules\pg-protocol\src\parser.ts:103:30)
at Socket.<anonymous> (d:\Projetos-VSCODE\Crud-tsnode\node_modules\pg-protocol\src\index.ts:7:48)
at Socket.emit (node:events:390:28)
at Socket.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:199:23) {
length: 168,
severity: 'FATAL',
code: '28000',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'auth.c',
line: '496',
routine: 'ClientAuthentication'
},
original: error: no pg_hba.conf entry for host "186.194.175.39", user "xuavcbfkstvqrm", database "d3t76co8ov8vqm", SSL off
at Parser.parseErrorMessage (d:\Projetos-VSCODE\Crud-tsnode\node_modules\pg-protocol\src\parser.ts:369:69)
at Parser.handlePacket (d:\Projetos-VSCODE\Crud-tsnode\node_modules\pg-protocol\src\parser.ts:188:21)
at Parser.parse (d:\Projetos-VSCODE\Crud-tsnode\node_modules\pg-protocol\src\parser.ts:103:30)
at Socket.<anonymous> (d:\Projetos-VSCODE\Crud-tsnode\node_modules\pg-protocol\src\index.ts:7:48)
at Socket.emit (node:events:390:28)
at Socket.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:199:23) {
length: 168,
severity: 'FATAL',
code: '28000',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'auth.c',
line: '496',
routine: 'ClientAuthentication'
}
}
What is missing in my code to connect correctly?, doing this test and being able to connect to the postgresql database on heroku, I will upload the application and I believe that the CRUD that needs to access the route and add the user will work, but I have to do it this connection to the heroku database, but I'm not getting it

Delete request nodejs typescript

My code looks like this:
app.delete("/delete/:id", urlencodedParser, (req: Request, res: Response) => {
const id: any = req.params
console.log(req.params)
pool.query(`DELETE FROM allusers WHERE userid = $1`, [id]).catch(err => {
console.log(err)
})
})
But when I test it whit postman and go on e.g localhost:5000/delete/1 it logs:
error: syntax error at or near "["
at Parser.parseErrorMessage (C:\Users\prcap\Desktop\ucim\server\node_modules\pg-protocol\src\parser.ts:369:69)
at Parser.handlePacket (C:\Users\prcap\Desktop\ucim\server\node_modules\pg-protocol\src\parser.ts:188:21)
at Parser.parse (C:\Users\prcap\Desktop\ucim\server\node_modules\pg-protocol\src\parser.ts:103:30)
at Socket. (C:\Users\prcap\Desktop\ucim\server\node_modules\pg-protocol\src\index.ts:7:48)
at Socket.emit (events.js:375:28)
at Socket.emit (domain.js:470:12)
at addChunk (internal/streams/readable.js:290:12)
at readableAddChunk (internal/streams/readable.js:265:9)
at Socket.Readable.push (internal/streams/readable.js:204:10)
at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
length: 90,
severity: 'ERROR',
code: '42601',
detail: undefined,
hint: undefined,
position: '37',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'scan.l',
line: '1180',
routine: 'scanner_yyerror'
}
How can I fix this?

The db connection throws error in model unit test without explicit invoking

I am doing unit test on nodejs 12.x jest 25 app. When the unit test is started with npm test, at the end it throws an error about a startup file db.js which establishes db connection. Here is the db.js file:
const Sql = require("sequelize");
const db = new Sql('emps', 'postgres', `${process.env.DB_PASSWORD}`, {
host: 'localhost',
dialect: 'postgres',
port:5432,
} );
db
.authenticate() //<<<====this line causes the error
.then(() => {
console.log('DB connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
module.exports = db;
Here is the error at the end of jest unit test:
console.error startup/db.js:32
Unable to connect to the database: ConnectionError [SequelizeConnectionError]: password authentication failed for user "postgres"
at C:\D\code\js\emps_bbone\node_modules\sequelize\lib\dialects\postgres\connection-manager.js:182:24
at Connection.connectingErrorHandler (C:\D\code\js\emps_bbone\node_modules\pg\lib\client.js:194:14)
at Connection.emit (events.js:311:20)
at Socket.<anonymous> (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:134:12)
at Socket.emit (events.js:311:20)
at addChunk (_stream_readable.js:294:12)
at readableAddChunk (_stream_readable.js:275:11)
at Socket.Readable.push (_stream_readable.js:209:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
name: 'SequelizeConnectionError',
parent: error: password authentication failed for user "postgres"
at Connection.Object.<anonymous>.Connection.parseE (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:614:13)
at Connection.Object.<anonymous>.Connection.parseMessage (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:413:19)
at Socket.<anonymous> (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:129:22)
at Socket.emit (events.js:311:20)
at addChunk (_stream_readable.js:294:12)
at readableAddChunk (_stream_readable.js:275:11)
at Socket.Readable.push (_stream_readable.js:209:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
name: 'error',
length: 163,
severity: 'FATAL',
code: '28P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'd:\\pginstaller.auto\\postgres.windows-x64\\src\\backend\\libpq\\auth.c',
line: '336',
routine: 'auth_failed'
},
original: error: password authentication failed for user "postgres"
at Connection.Object.<anonymous>.Connection.parseE (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:614:13)
at Connection.Object.<anonymous>.Connection.parseMessage (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:413:19)
at Socket.<anonymous> (C:\D\code\js\emps_bbone\node_modules\pg\lib\connection.js:129:22)
at Socket.emit (events.js:311:20)
at addChunk (_stream_readable.js:294:12)
at readableAddChunk (_stream_readable.js:275:11)
at Socket.Readable.push (_stream_readable.js:209:10)
at TCP.onStreamRead (internal/stream_base_commons.js:186:23) {
name: 'error',
length: 163,
severity: 'FATAL',
code: '28P01',
detail: undefined,
hint: undefined,
position: undefined,
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'd:\\pginstaller.auto\\postgres.windows-x64\\src\\backend\\libpq\\auth.c',
line: '336',
routine: 'auth_failed'
}
}
The db.js is required in app's index.js:
require("./startup/db");
Since my unit tests only test models (the db connection works fine. The problem is likely process.env.DB_PASSWORD didn't return a value somehow.), I don't quite understand how the db connection test jumps in by itself and throws error.

Select * from table with NodeJS // ERROR: { error: relation "mytable" does not exist

I get the following error during my SELECT query from PostgreSQL DB
ERROR: { error: relation "free_subnets" does not exist
at Connection.parseE (/home/ec2-user/environment/node_modules/pg/lib/connection.js:604:11)
at Connection.parseMessage (/home/ec2-user/environment/node_modules/pg/lib/connection.js:401:19)
at Socket.<anonymous> (/home/ec2-user/environment/node_modules/pg/lib/connection.js:121:22)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
name: 'error',
length: 103,
severity: 'ERROR',
code: '42P01',
detail: undefined,
hint: undefined,
position: '15',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'parse_relation.c',
line: '874',
routine: 'parserOpenTable' }
If I use psql command and do a select there, it works.
\dt provides me the relation:
Schema | Name | Type | Owner
-------------+--------------+-------+----------
subnet_calc | free_subnets | table | postgres
Search_path has been set to: subnet_calc, public
I've created the table with user postgres, so I am the owner of the schema subnet_calc and the table free_subnets.
My dummy nodejs code is the following:
const pg = require('pg');
const cs = 'postgres://postgres:password#anydb.rds.amazonaws.com:5432/subnet_calculator';
const client = new pg.Client(cs);
client.connect();
client.query('SELECT * FROM subnet_calc.free_subnets', function(result) {
console.log(result);
});
What could be the issue?
Thanks for your help in advance!
I could resolve the issue. It was my failure, the tables were created in DB postgres not in subnet_calculator.
I will close this question.
you need to set search path for it like this:
db.query("SET search_path TO 'subnet_calc';")
and then do the query :
const query = `SELECT * FROM free_subnets`
db.query(query)

Resources