Questions tagged [pg-promise]
Complete access layer to node-postgres via Promises/A+.
523
questions
59
votes
3
answers
48k
views
Multi-row insert with pg-promise
I would like to insert multiple rows with a single INSERT query, for example:
INSERT INTO tmp(col_a,col_b) VALUES('a1','b1'),('a2','b2')...
Is there a way to do this easily, preferably for an array ...
39
votes
2
answers
34k
views
Verify database connection with pg-promise when starting an app
I am building an express application that connects to a postgres database using the pg-promise module.
I would like to ensure that the database connection is successful when starting the application ...
35
votes
4
answers
23k
views
pg-promise returns integers as strings
I have this simple query to a table that contains a column of type bigint.
However when I query it, pg-promise returns this column's values as a string. I can't find info about that in the ...
35
votes
4
answers
26k
views
How do I setup Babel 6 with Node JS to use ES6 in my Server Side code?
I have read several times the documentation provided at :
Node API Babel 6 Docs
I'm starting out learning pg-promise following the Learn by Example tutorial and would prefer to work with ES6 and ...
28
votes
2
answers
21k
views
Where should I initialize pg-promise
I just started to learn nodejs-postgres and found the pg-promise package.
I read the docs and examples but I don't understand where should I put the initialization code? I using Express and I have ...
25
votes
1
answer
18k
views
Inserting multiple records with pg-promise
I have a scenario in which I need to insert multiple records. I have a table structure like id (it's fk from other table), key(char), value(char). The input which needs to be saved would be array of ...
24
votes
1
answer
11k
views
node-postgres vs pg-promise for Nodejs Application
I'm going to build a Nodejs application with Postgresql as back end. I'm not going to use ORMs like Sequelize due to poor documentation and performance problems or any other ORM - ORM is an anti-...
14
votes
1
answer
20k
views
,column <columnName> is of type jsonb but expression is of type text[]
Have array as below, needs to be saved in JSONB column:
[{"FoodType":"veg","pref":"High"}
,{"FoodType":"sea food","pref":"Medium"}
,{"FoodType":"Chicken","pref":"Low"}]
I simply pass the req.body ...
13
votes
1
answer
14k
views
How to get results from multiple queries at once with pg-promise?
Currently I have the following code to get the results of two queries
dbro.many("SELECT geoname_id, country_name FROM paises WHERE locale_code=$1 LIMIT 10",data.lang)
.then(function(countriesData){...
13
votes
2
answers
8k
views
Pass an array of integers in array of parameters
I am trying to pass an array of parameters in pg-promise's array of parameters, as recommended in pg-promise docs.
db.any("SELECT fieldname FROM table WHERE fieldname = $1 AND fieldname2 IN ($2)",
...
13
votes
1
answer
7k
views
Log specific postgresql query using pg-promise
I am using pg-promise package with Nodejs to execute PostgreSQL queries. I want to see the queries executed. Only specific queries, say, just one query that I want to debug.
I can see that one ...
12
votes
4
answers
10k
views
NodeJS, promises, streams - processing large CSV files
I need to build a function for processing large CSV files for use in a bluebird.map() call. Given the potential sizes of the file, I'd like to use streaming.
This function should accept a stream (a ...
9
votes
1
answer
4k
views
pg-promise number of updated rows
I am using the pg-promise NodeJS module. At some point, I am updating one of my database's table. I would like to know how many rows have been updated by the query, is it possible?
Below is part of ...
9
votes
1
answer
5k
views
NodeJS and pg-promise, catch PostgreSQL exceptions
I'm running NodeJS and pg-promise with a PostgreSQL backend.
I've created my own TRIGGER which throws an exception in some cases. Things work fine at this end.
But with pg-promise I have trouble ...
8
votes
3
answers
5k
views
How to import pg-promise using ES6 syntax?
I am trying to set up a Postgres database in a nodejs server using ES6 syntax, but I don't think I'm importing and initializing pg-promise properly. If I were using common js sytax I would do the ...
8
votes
1
answer
7k
views
How to set query_timeout in relation to statement_timeout?
We can set 2 timeouts for the Client:
statement_timeout: number of milliseconds before a statement in query will time out, default is no timeout
query_timeout
number of milliseconds before a query ...
8
votes
2
answers
5k
views
Using node.js and promise to fetch paginated data
Please keep in mind that I am new to node.js and I am used with android development.
My scenario is like this:
Run a query against the database that returns either null or a value
Call a web ...
8
votes
1
answer
5k
views
Synchronous programming using pg-promise
I am new to node.js and pg-promise coming from a synchronis programming background it requires a new thinking.
I would to start with a scenario of running database initialisation before the main ...
7
votes
1
answer
6k
views
Is it ok to be setting rejectUnauthorized to false in production PostgreSQL connections?
We recently moved to Heroku and upon attempting to connect our apps to the DB, it kept rejecting our queries with the message "Self signed certificate". Passing in rejectUnauthorized solved ...
7
votes
2
answers
6k
views
how do I close pg-promise connection in node.js after all tests have run in jest?
I need to close a PG-Promise database connection after testing a function in Jest.
This is initialised in one place(db.js) and required everywhere it is needed. In the case of the code below, it is ...
7
votes
2
answers
2k
views
WHERE col IN Query with empty array as parameter
From example where-col-in example and this answer, WHERE IN clauses should have query with parameters with following syntax
const response = await db.any('SELECT * FROM table WHERE id IN ($1:csv)', [...
7
votes
2
answers
12k
views
Node.js - PostgreSQL - could not determine data type of parameter $1 error
I'm trying to create a PostgreSQL prepared statement using node.js pg npm package. However, I keep getting an error:
could not determine data type of parameter $1
function promiseQuery(sql, ...
6
votes
1
answer
10k
views
pg-promise - Error operator does not exist: bigint = bigint[]
I am trying to run the query:
let query =
`
DELETE FROM
${table_name}
WHERE
_id IN ($1::bigint[])
AND
account_id = $2
`
let ...
6
votes
2
answers
4k
views
Optional column update if provided value for column is not null
I have following table:
CREATE TABLE IF NOT EXISTS categories
(
id SERIAL PRIMARY KEY,
title CHARACTER VARYING(100) NOT NULL,
description CHARACTER VARYING(200) NULL,
category_type ...
6
votes
3
answers
2k
views
Combine nested loop queries to parent array result - pg-promise
I'm new to node(express) and pg-promise, and have not been able to figure out how to add the result of each nested query(loop) into the main json array result query.
I have two tables: Posts and ...
5
votes
2
answers
9k
views
Connection pool using pg-promise
I'm using Node js and Postgresql and trying to be most efficient in the connections implementation.
I saw that pg-promise is built on top of node-postgres and node-postgres uses pg-pool to manage ...
5
votes
2
answers
4k
views
PostgreSQL multi-value upserts
is it possible to perform a multi-value upsert in PostgreSQL? I know multi-value inserts exist, as do the "ON CONFLICT" key words to perform an update if the key is violated... but is it possible to ...
5
votes
1
answer
2k
views
INSERT ON CONFLICT DO UPDATE using pg-promise helpers for multi row insert/update
I am trying to insert multiple rows to pgsql database using pg-promise. In my case, few records, which I am trying to insert, may exist already in the table. In such case, I need to update them. By ...
5
votes
1
answer
5k
views
Is it possible to "CREATE DATABASE ..." with pg-promise and PostgreSQL (9.5)?
Question is:
1) Can pg-promise be used to create a new database (schemas, et. al.)?
Using node package 'pg-promise' I can't seem to figure out if it's possible to create a new database. I can ...
5
votes
1
answer
2k
views
Possible to add dynamic WHERE clause with a QueryFile?
I have a complex query stored in an SQL file and I would like to reuse it for various routes but change up the WHERE clause depending on the route. This would be instead of having a large complex ...
5
votes
1
answer
4k
views
JavaScript (Postgres DB) - How to use a prepared statement with an array as parameter in the WHERE IN ( ) clause
I am currently using the database class from http://vitaly-t.github.io/pg-promise/Database.html and trying to implement an Update statement using a PreparedStatment on my Postgres DB while having an ...
5
votes
1
answer
4k
views
How to return insert query result values using pg-promise helpers
I am using pgp.helpers.insert to save data into PostgreSQL which is working well. However, I need to return values to present in a response. I am using:
this.collection.one(this.collection.$config....
5
votes
1
answer
5k
views
Postgresql Catching Transaction Error and Rollback
I am using pg-promise to run my SQL queries. The queries themselves are stored in external .sql files.
When I execute a transaction, Postgres will abort the transaction if an error occurs (as ...
5
votes
1
answer
2k
views
Pg-promise performance boost : ON CONFLICT
I'm trying to follow the performance pattern recommended by the pg-promise library author here.
Basically Vitaly recommends to do so with inserts :
var users = [['John', 23], ['Mike', 30], ['David',...
5
votes
2
answers
2k
views
how should i do asynchronous unit testing?
I am using pg-promise. i am learnner please excuse if it seems trivial to you. how can i wrtie unit test for. it errors out data is undefined. i have been making connection in js file and export that ...
5
votes
1
answer
2k
views
Return in pg-promise
I created a separate file with all my queries using pg-promise module for Node. While for most of them I just use req, res after a query, for one I want to return a value back. It does not work. It ...
5
votes
2
answers
2k
views
Next.js API routes with pg-promise
I’m using the excellent pg-promise library inside a Next.js app to interface with a Postgres database deployed on AWS. Specifically, I’m using the API routes feature, where folders inside /pages/api ...
5
votes
2
answers
3k
views
How to mock pg-promise library with jest
I am trying to mock the pg promise library. I want to be able mock return whether the promise rejects or resolves. Here is an example function and test:
const pgp = require('pg-promise')({});
const ...
4
votes
1
answer
6k
views
pg-promise: use result of one query in next query within a transaction
I am node.js with pg-promise for postgres, trying to do a transaction with 2 inserts in sequence. The result id of the 1st insert should be used in the next insert in the transaction.
Rollback if any ...
4
votes
2
answers
3k
views
Node express & pg-promise - db.any is not a function
I have this error :
TypeError: db.any is not a function
I'm using node JS, with pg-promise and express.
Here is the queries.js file (db) :
const promise = require('bluebird');
var options = {
...
4
votes
3
answers
3k
views
What can cause "idle in transaction" for "BEGIN" statements
We have a node.js application that connects via pg-promise to a Postgres 11 server - all processes are running on a single cloud server in docker containers.
Sometimes we hit a situation where the ...
4
votes
2
answers
6k
views
Pg-promise: proper way of returning result of query
I want to check whether a username is already in use using pg-promise.
I use the following query:
this.db.one('SELECT EXISTS(SELECT 1 FROM users WHERE username = $1)', username);
I am trying to ...
4
votes
2
answers
2k
views
pg-promise in AWS lambda
I am having quite a number of issues using pg-promise with AWS lambda. I wanted to understand how do I solve these issues.
The library advises you to create one instance of the Database object and ...
4
votes
1
answer
13k
views
Query formatting for Parameterized Queries
I am using pg-promise to execute select query with like clause in PostgreSQL. Unfortunately the query is failing with error code as 08P01 and error message
bind message supplies 1 parameters, but ...
4
votes
1
answer
3k
views
How to make pg-promise return rows as arrays?
How can we make pg-promise return an array of rows from a query, as opposed to array of row objects?
4
votes
1
answer
2k
views
pg-promise: Pass a function as parameter to func()
I am using pg-promise to access our postgres database. I want to call a stored proc foo(geom) that accepts a geometry datatype (PostGIS). I only have lats/lngs to start with though, so I want to ...
4
votes
1
answer
2k
views
Having Trouble Initializing Postgresql Database Through PG-Promise
I'm completely new to Express/Postgresql and I'm trying to learn them to create a web application. After some poking around, I decided that I wanted to develop my back-end with TypeScript. I ...
4
votes
1
answer
2k
views
NodeJS, pg-promise and array as parameter
Running NodeJS and pg-promise.
I've got an array:
let my_array = new array('x', 'y', 'z');
I'm trying to pass this as a parameter to my PostgreSQL query with pg-promise:
db_webhooks.any('SELECT ...
4
votes
1
answer
3k
views
Is there a way to Camel Case the column names returned from PostgreSQL using pg-promise?
When we execute queries to our PostgreSQL Database and receive responses, we then pass these responses to our clientside to display/work with.
Example:
const response = [
{
first_name: 'Bob',
...
4
votes
1
answer
1k
views
Get a parents + children tree with pg-promise
I use the pg-promise library with bluebird for making dependent queries.
I have two tables, a and b, looking like this:
| a | | b |
|-------| |-------|
| a_id | | b_id |
| ...