Import sql file in node.js and execute against PostgreSQL

You can just separate consequent queries with a semicolon when passed to client.query That works: var pg = require(‘pg’); pg.connect(‘postgres://test:test@localhost/test’, function(err, client, done){ client.query(‘CREATE TABLE test (test VARCHAR(255)); INSERT INTO test VALUES(\’test\’) ‘); done(); }); And consequently, that works too: var pg = require(‘pg’); var fs = require(‘fs’); var sql = fs.readFileSync(‘init_database.sql’).toString(); pg.connect(‘postgres://test:test@localhost/test’, function(err, client, … Read more

How do I properly insert multiple rows into PG with node-postgres?

Use pg-format like below. var format = require(‘pg-format’); var values = [ [7, ‘john22’, ‘john22@gmail.com’, ‘9999999922’], [6, ‘testvk’, ‘testvk@gmail.com’, ‘88888888888’] ]; client.query(format(‘INSERT INTO users (id, name, email, phone) VALUES %L’, values),[], (err, result)=>{ console.log(err); console.log(result); });

when to disconnect and when to end a pg client or pool

First, from the pg documentation*: const { Pool } = require(‘pg’) const pool = new Pool() // the pool with emit an error on behalf of any idle clients // it contains if a backend error or network partition happens pool.on(‘error’, (err, client) => { console.error(‘Unexpected error on idle client’, err) // your callback here … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)