node-postgres

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

跟風遠走 提交于 2019-12-09 07:44:25
问题 My stack is node, express and the pg module. I really try to understand by the documentation and some outdated tutorials. I dont know when and how to disconnect and to end a client. For some routes I decided to use a pool. This is my code const pool = new pg.Pool({ user: 'pooluser',host: 'localhost',database: 'mydb',password: 'pooluser',port: 5432}); pool.on('error', (err, client) => { console.log('error ', err); process.exit(-1); }); app.get('/', (req, res)=>{ pool.connect() .then(client =>

(node-postgres, pg) Proper insertion of table name

本秂侑毒 提交于 2019-12-08 17:45:24
问题 How does one correctly provide the table name if the name can be dynamically determined and still prevent SQL injection attacks? For example: The following works but I believe is insecure: dbclient.query("INSERT INTO " + table_name + " VALUES ($1, $2, $3)", [value_a, value_b, value_c]) What I would like equivalently (but does not work) is: dbclient.query("INSERT INTO $1 VALUES ($2, $3, $4)", [table_name, value_a, value_b, value_c]) Edit: I am using node-postgres : https://github.com/brianc

PostgreSQL connection via javascript

送分小仙女□ 提交于 2019-12-08 13:39:14
问题 I am searching for a way to connect to postgresql directly in the browser. Im trying to utilize nodejs and browserify but have had no luck so far with the bundling. Whenever I compile a script that contains a require('pg') it specifically states in the browser: Cannot find module '/node_modules/pg/lib/client' the browser tells me afterwards that he is not able to find the modules that pg requires. Maybe i need to bundle pg with browserify before ? I appreciate if somebody has an idea on how

node-postgres will not insert data, but doesn't throw errors either

爷,独闯天下 提交于 2019-12-07 13:33:32
问题 I'm using the node-postgres module for node.js and I have a problem inserting data. The function: function addItems(listId, listItems, handle) { if (!listItems) { handle(listId); return; } var client = dbConnector.getClient(), important, dateArray, dateString, i, prepStatement; client.connect(); for (i in listItems) { console.log(listItems[i]); dateArray = listItems[i].itemdate.split('-'); dateString = dateArray[1] + '-' + dateArray[0] + '-' + dateArray[2]; if (listItems[i].important) {

does node-postgres support multiple resultsets

旧巷老猫 提交于 2019-12-07 05:53:55
问题 I have a PostgresQL function that returns multiple resultsets. I can extract these resultsets in .net without a problem (so I know my function works correctly), but I am having trouble doing so with node-postgres. The result object returns an array of 7 items which matches the number of datasets returned. In Node, the each of the 7 rows simply contains a string of <unnamed portal 1> . connection.query("BEGIN"); connection.query({text: "SELECT getoperationaldatasetmodel($1)", values :

How to convert MySQL-style question mark `?` bound parameters to Postgres-style `$1` bound parameter

倖福魔咒の 提交于 2019-12-06 13:24:21
I am converting an existing project from MySQL to Postgres. There are quite a few raw SQL literals in the code that use ? as a placeholder, e.g. SELECT id FROM users WHERE name = ? But I get this error: DB query error: error: operator does not exist: character varying = ? I don't want to convert all my existing SQL from ? to postgres-style operators like $1 . Is there some way of having node-postgres accept the question marks instead, or an utility that can convert to postgres style params? Note that some sort of Regex-based hack is not acceptable because question marks can be inside quotes,

node server can't connect to postgres db

对着背影说爱祢 提交于 2019-12-06 02:16:38
问题 I recently switched from MySQL to postgres as my database for an node.js project. While I'm able to reach my remote postgres database from my local pgAdmin III (OSX) client, so far I've been unable to connect to my database through node.js. I'm sure that the credentials I entered for pgAdmin and my node.js were exactly the same. The other thing I've tried was setting my local ipadress to trust in stead of md5 in the pg_hba.conf at my database server. Is there anything I did wrong? My

node-postgres will not insert data, but doesn't throw errors either

試著忘記壹切 提交于 2019-12-06 00:34:38
I'm using the node-postgres module for node.js and I have a problem inserting data. The function: function addItems(listId, listItems, handle) { if (!listItems) { handle(listId); return; } var client = dbConnector.getClient(), important, dateArray, dateString, i, prepStatement; client.connect(); for (i in listItems) { console.log(listItems[i]); dateArray = listItems[i].itemdate.split('-'); dateString = dateArray[1] + '-' + dateArray[0] + '-' + dateArray[2]; if (listItems[i].important) { important = 'true'; } else { important = 'false'; } prepStatement = { name: 'insert task', text: 'INSERT

does node-postgres support multiple resultsets

吃可爱长大的小学妹 提交于 2019-12-05 10:30:32
I have a PostgresQL function that returns multiple resultsets. I can extract these resultsets in .net without a problem (so I know my function works correctly), but I am having trouble doing so with node-postgres. The result object returns an array of 7 items which matches the number of datasets returned. In Node, the each of the 7 rows simply contains a string of <unnamed portal 1> . connection.query("BEGIN"); connection.query({text: "SELECT getoperationaldatasetmodel($1)", values : [clientid]}, function(err, results) { if (err) { connection.query("COMMIT"); self.pool.release(connection);

postgres:get executable query from query with parameters

北城以北 提交于 2019-12-04 20:24:37
Is there any way to get executable query from query with $ parameters.Actually its weird but i want to store executable query in database.A complete query without parameters($1,$2,$3) i am using node-postgres pg.connect(conString, function(err, client, done) { console.log('Executing Insert query'); client.query('insert into tablename(column1,column2,column3) values($1,$2,$3)',["data1","data2","data3"], function(err, result) { done(); console.log('finished executing Insert query'); }); }); this is what i need insert into tablename(column1,column2,column3) values("data1","data2","data3") pg