I'm writing a lambda function to connect to a postgress DB that I have on an EC2 instance. I've been utilizing the 'pg' library for connecting as found in their documentation, however, my function keeps skipping over the establish connection piece of my method and just continuing and exiting without accomplishing anything.
const client = new Client({
user: 'user',
host: 'xxxx.xxx.xxxx',
database: 'dbname',
password: 'password',
port: 5432,
})
client.connect(err => {
if (err) {
console.error('connection error', err.stack)
} else {
console.log('connected')
}
})
client.query('select count(*) from "Product"', (error, results) => {
if (error) {
console.log("Error when trying to query");
throw error
}
console.log(results.rows)
})
I'm going exactly by the methods that the 'pg' documentation says (https://node-postgres.com/features/connecting), but can't figure out what is going wrong here. I'm using serverless with nodejs12.x for this function.
You are not waiting for the connection to be established before querying. Try this:
const client = new Client({
user: 'user',
host: 'xxxx.xxx.xxxx',
database: 'dbname',
password: 'password',
port: 5432,
})
return client.connect(err => {
if (err) {
console.error('connection error', err.stack)
} else {
console.log('connected')
return client.query('select count(*) from "Product"', (error, results) => {
if (error) {
console.log("Error when trying to query");
throw error
}
console.log(results.rows)
})
}
})
Although, if your can, create a promise chain as it is probably easier to manage like so:
const client = new Client({
user: 'user',
host: 'xxxx.xxx.xxxx',
database: 'dbname',
password: 'password',
port: 5432,
})
return client.connect().then(()=>{
return client.query('select count(*) from "Product"')
}).then((results)=>{
console.log(results.rows)
}).catch((err)=>{
console.error('error', err.stack? err.stack : err)
})
I say use a promise chain if you can because Im not sure what the pg library returns on connect and query..
Hope this helps!
Related
I am using Knex query builder and Postgres as my data base.
I need to get the output from a raise notice or raise info from a Postgres function or procedure.
raise notice 'This is my Log';
we need to set notice after pool created, for that we need to use afterCreate function.
for your references
https://github.com/knex/knex/issues/4241
The sample code is below.
var pConn = {
host: 'SERVER',
port: 'PORT',
user: 'USER',
password: 'PASSWORD',
database: 'DATABAENAME'
};
var query = "select * from pglog_test()";
try {
const knex = require('knex')({
client: 'pg',
connection: pConn,
pool: {
afterCreate: function (conn, done) {
conn.query('select 1 as result', function (err, result) {
conn.on('notice', function (msg) {
// here we can get the pg procedure logs (raise notice/raise info)
console.log('logs from postgress' + msg);
});
done(err, conn);
});
}
}
});
// procedure calling
knex.raw(query).then(function (result) {
console.log(result);
}).catch(function (error) {
console.log(error);
});
} catch (error) {
console.log(error);
}
I am using Sequelize with postgres database.
But authenticate() function does not send any response(whether it is success or failed)
Here is my code.
const connection = new Sequelize('BLIG', 'postgres', 'admin', {
host: 'localhost',
dialect: 'postgres',
define: {
timestamps: false
}
});
app.use('/', (req, res, next) => {
console.clear();
connection.authenticate()
.then(
() => { console.log("Database connected..."), next() },
error=>{console.log("Database connection error",error)}
)
.catch(err => { console.log('database connection error', err), res.redirect('/error') });
})
If anyone knows this issue, help me please.
update your "pg" node package to 8.5.1 or above version using
npm i pg#8.5.1
I am getting error Invalid object name dbo.tbl_user.while running a query on the Azure database from a nodejs app.My code which runs is following.
const pool = new sql.ConnectionPool(DatabaseConfig.config);
pool.connect().then(function () {
//6.
debugger;
var request = new sql.Request(pool);
//7.
request.query("select * from [dbo].[tbl_user]").then(function (recordSet) {
debugger;
console.log(recordSet);
sql.close();
}).catch(function (err) {
//8.
debugger;
console.log(err);
sql.close();
});
}).catch(function (err) {
//9.
debugger;
console.log(err);
});
My connection string is :
config: {
server: 'azureserver',
options: {
database: 'cccc',
encrypt: true,
port: 1433
},
user: 'ccc',
password: 'ccccc'
}
what is the mistake I am doing here? Thanks in advance for the help.
I tested your code on my side but did not reproduce your issue.
Here is my test code:
const sql = require('mssql')
const config = {
server: '***.database.windows.net',
database: '***',
user: '***',
password: '***',
options: {
database: '***',
encrypt: true,
port: 1433
}
}
const pool = new sql.ConnectionPool(config);
pool.connect().then(function () {
//6.
debugger;
var request = new sql.Request(pool);
//7.
request.query("select * from [dbo].[Student]").then(function (recordSet) {
debugger;
console.log(recordSet);
sql.close();
}).catch(function (err) {
//8.
debugger;
console.log(err);
sql.close();
});
}).catch(function (err) {
//9.
debugger;
console.log(err);
});
Query result on the portal:
Run query.js :
You need to make sure the table name is correct, it's not related to spelling case.
Hope it helps you.
So, I use the pg module in node 8.11.1 / express 4.16.3 / pg 7.4.2
I try to use the pool for my front-end (just selects) and the examples are somewhat confusing.
In connecting it uses just a new Pool and then it shows that I have to do pool.end()
const pool = new Pool({
user: 'dbuser',
host: 'database.server.com',
database: 'mydb',
password: 'secretpassword',
port: 3211,
})
pool.query('SELECT NOW()', (err, res) => {
console.log(err, res)
pool.end()
})
I made my code like that and it prints Error: Cannot use a pool after calling end on the pool If I do the same query a couple of times. So, no pool.end()
In queries there is no disconnection in the examples (?)
I finally made my code like the pooling. It shows the pool.on('error', (err, client) => { function and then it uses client.release() in the pool, since "pool.query delegates directly to client.query internally" I guess?
So, what is the right way to use pool in the pg and how to disconnect after each query or failure? I came up with this
const pool = new pg.Pool({
user: 'user',
host: 'localhost',
database: 'myProject',
password: 'secret',
port: 5432
});
pool.on('error', (err, client) => {
console.error('error on client', err, 'on client' , client);
process.exit(-1);
});
app.get('/', (req, res)=>{
pool.connect()
.then(client => {
return client.query('select name from table')
.then(resolved => {
client.release();
res.render('index',{'testData': resolved.rows});
})
.catch(e => { //return client.query
client.release();
res.render('index',{'errorData': e});
})
.catch(e => { //pool.connect()
client.release();
res.render('index',{'errorData': e});
})
})
});
I dont know if this can be shorter in any way. Like, for example if the catch(e => { ////pool.connect()... is needed or it is covered by pool.on('error', (err, client) => {...
Also, it could be a lot sorter if it was like
const pool = new pg.Pool({
user: 'user',
host: 'localhost',
database: 'myProject',
password: 'secret',
port: 5432
});
app.get('/', (req, res)=>{
pool.query('...')
.then(resolved => {
pool.end();// pool disconnect ???
res.render('index',{
'testData': resolved.rows
});
})
.catch(e => {
pool.end();// pool disconnect ???
res.render('index',{
'testData': e
});
})
});
But I dont know if this is right because there is no pool.connect , there is no client returned from that connection and there is no function to disconnect the pool (only to end it, that ends up again with Error: Cannot use a pool after calling end on the pool).
Please advice on the right pool usage and syntax
Thanks
I ran into this kind of Problem too with another npm package for mssql.
After some trail and error i decided to go for a singelton (static would also be possible) class which handles the connection.
Now you just have to call one function before each query in order to create a new connection pool or receive the present one.
Something like this:
let _this = {};
let connectionPool = null;
function getConnection(){
if(connectionPool){
return connectionPool
} else {
connectionPool = new pg.Pool({
user: 'user',
host: 'localhost',
database: 'myProject',
password: 'secret',
port: 5432
});
return connectionPool;
}
}
function closeConnection(){
// close connection here
}
_this.getConnection = getConnection;
_this.closeConnection = closeConnection;
module.exports = _this;
For example, in the following code (using node-postgres), we connect to the client, query and then end.
const { Pool, Client } = require('pg')
const pool = new Pool({
user: 'dbuser',
host: 'database.server.com',
database: 'mydb',
password: 'secretpassword',
port: 3211,
})
pool.query('SELECT NOW()', (err, res) => {
console.log(err, res)
pool.end()
})
const client = new Client({
user: 'dbuser',
host: 'database.server.com',
database: 'mydb',
password: 'secretpassword',
port: 3211,
})
client.connect()
client.query('SELECT NOW()', (err, res) => {
console.log(err, res)
client.end()
})
Does that mean we should create a connection each time we intend to query the database ? Is there a faster way ?
Would you suggest using something besides node-postgres?
I will suggest sequelize ORM so you will don't need to worry about other database operation. It also supports PostgreSQL and will maintain and end connection for you its mean just need to create connection and Sequelize will do remaining for you boom it's amazing.
Sequelize is a promise-based ORM for Node.js v4 and up. It supports the dialects PostgreSQL, MySQL, SQLite and MSSQL and features solid transaction support, relations, read replication and more.
const sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'mysql'|'sqlite'|'postgres'|'mssql',
pool: {
max: 5,
min: 0,
idle: 10000
},
// SQLite only
storage: 'path/to/database.sqlite'
});
// Or you can simply use a connection uri
const sequelize = new Sequelize('postgres://user:pass#example.com:5432/dbname');
You can use the .authenticate() function like this to test the connection.
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
For more detail http://docs.sequelizejs.com/