How to use the Pool function from pg
Find comprehensive JavaScript pg.Pool code examples handpicked from public code repositorys.
pg.Pool is a PostgreSQL connection pool that allows multiple connections to the database to be created and managed efficiently.
11 12 13 14 15 16 17 18 19 20 21
return connect(json, cb); } function connectPool(json, cb) { var numConnections = json.connectionLimit || 0; var pool = new Pool({ host: json.host, port: json.port, user: json.user, database: json.database,
+ 2 other calls in file
GitHub: yingziwu/mastodon
139 140 141 142 143 144 145 146 147 148
const app = express(); app.set('trust proxy', process.env.TRUSTED_PROXY_IP ? process.env.TRUSTED_PROXY_IP.split(/(?:\s*,\s*|\s+)/) : 'loopback,uniquelocal'); const pgPool = new pg.Pool(Object.assign(pgConfigs[env], dbUrlToConfig(process.env.DATABASE_URL), { max: process.env.DB_POOL || 10, connectionTimeoutMillis: 15000, ssl: !!process.env.DB_SSLMODE && process.env.DB_SSLMODE !== 'disable', }));
How does pg.Pool work?
pg.Pool
is a class provided by the pg
library in Node.js that creates a pool of PostgreSQL client connections that can be shared across multiple requests, enabling efficient use of resources and reducing the overhead of creating and closing connections for each request. When a client requests a connection from the pool, it returns a new or an existing connection from the pool based on availability, and releases it back to the pool after use. The pool also automatically manages idle clients, connection errors, and reconnection attempts.
33 34 35 36 37 38 39 40 41 42 43
}; connection.connect = async function (options) { const { Pool } = require('pg'); const connOptions = connection.getConnectionOptions(options); const db = new Pool(connOptions); await db.connect(); return db; };
Ai Example
1 2 3 4 5 6 7 8 9
const { Pool } = require("pg"); const pool = new Pool({ user: "db_user", host: "localhost", database: "my_database", password: "my_password", port: 5432, });
This creates a new Pool object with the provided database configuration options. Once the Pool object is created, you can use it to make queries to the database using the pool.query() method.
137 138 139 140 141 142 143 144 145 146
const app = express(); app.set('trust proxy', process.env.TRUSTED_PROXY_IP ? process.env.TRUSTED_PROXY_IP.split(/(?:\s*,\s*|\s+)/) : 'loopback,uniquelocal'); const pgPool = new pg.Pool(pgConfigFromEnv()); const server = http.createServer(app); const redisNamespace = process.env.REDIS_NAMESPACE || null; const redisParams = {
9 10 11 12 13 14 15 16 17 18 19 20 21
const CryptoJS = require('crypto-js') const freesoundKey = process.env.FREESOUND_KEY const sndEdit = new SoundEditor(freesoundKey) const pool = new Pool({ connectionString: process.env.DATABASE_URL }) const query = async function (sql) {
+ 2 other calls in file
2 3 4 5 6 7 8 9 10 11 12
const pool = undefined module.exports = function connection (){ // Adding Singleton pattern here if(!this.pool) { this.pool = new Pool({ user: 'postgres', host: 'localhost', database: 'postgres', password: '1234',
+ 5 other calls in file
10 11 12 13 14 15 16 17 18 19 20
// crear rutas privadas / publicas app.use(cors()) app.use(express.urlencoded({ extended: true })) app.use(express.json({ extended: true })) const clientPoll = new Pool({ user: process.env.PGUSER, host: process.env.PGHOST, database: process.env.PGDATABASE, port: process.env.PGPORT,
63 64 65 66 67 68 69 70 71 72 73 74
'Origin, X-Requested-With, Content-Type, Accept' ) next() }) const db = new Pool(config.database) const log = (msg) => { let now = new Date() console.log(
3 4 5 6 7 8 9 10 11 12 13 14
const app = express(); app.use(express.json()); app.use(cors()); const db = new Pool({ connectionString: 'postgres://admin:eWVvSRAMe4wkCbNkIBTcuDhBGHIpvwWu@dpg-cgbe7hl269v4icrcvid0-a.oregon-postgres.render.com/testing_g1yp', ssl: { rejectUnauthorized: false } });
GitHub: WaffleCollege/team-i
32 33 34 35 36 37 38 39 40 41 42
const auth = getAuth(firebaseapp); const provider = new GithubAuthProvider(); const db = getFirestore(firebaseapp); provider.addScope("repo"); var pool = new pg.Pool({ database: process.env.PG_DATABASE, user: process.env.PG_USER, password: process.env.PG_PASSWORD, host: process.env.PG_HOST,
4 5 6 7 8 9 10 11 12 13 14 15
const NotFoundError = require('../../exceptions/NotFoundError'); const {mapSongs} = require('../../utils/mapSongs'); class SongsService { constructor() { this._pool = new Pool(); } async addSong({title, year, genre, performer, duration, albumId}) { const id = `song-${nanoid(16)}`;
+ 2 other calls in file
GitHub: JohnKeigo/mastodon
146 147 148 149 150 151 152 153 154 155
const app = express(); app.set('trust proxy', process.env.TRUSTED_PROXY_IP ? process.env.TRUSTED_PROXY_IP.split(/(?:\s*,\s*|\s+)/) : 'loopback,uniquelocal'); const pgPool = new pg.Pool(Object.assign(pgConfigs[env], dbUrlToConfig(process.env.DATABASE_URL))); const server = http.createServer(app); const redisNamespace = process.env.REDIS_NAMESPACE || null; const redisParams = {
33 34 35 36 37 38 39 40 41 42
throw (0, index_1.createException)(e); } } exports.confirmOrder = confirmOrder; async function updateProductInventory(orderId, userId) { const connection = await new pg_1.Pool(posgre_1.PostgreSQLConfig); try { console.log("UPDATING INVENTORY"); await connection.query(`begin`); let productsId = await connection.query(`select productid, quantity
+ 44 other calls in file
GitHub: mioriimai/wearly
5 6 7 8 9 10 11 12 13 14 15
const ClientError = require('./client-error'); const pg = require('pg'); const uploadsMiddleware = require('./uploads-middleware'); // create a database connection object to use the pg package. const db = new pg.Pool({ connectionString: process.env.DATABASE_URL, ssl: { rejectUnauthorized: false }
GitHub: WebsyIO/websy-designs
72 73 74 75 76 77 78 79 80 81 82
` } class PGHelper { constructor () { const pool = new pg.Pool(config) this.pool = pool this.onReadyAuthCallbackFn = null this.onReadyShopCallbackFn = null this.options = { entityConfig: {}, fieldValueSeparator: ':' }
+ 5 other calls in file
GitHub: Fmlog/storefront-api
32 33 34 35 36 37 38 39 40 41
console.log(POSTGRES_HOST); console.log(POSTGRES_USER); console.log(POSTGRES_PASSWORD); var client; if (ENV === 'test') { client = new pg_1.Pool({ database: POSTGRES_TEST_DB, host: POSTGRES_HOST, user: POSTGRES_USER, password: POSTGRES_PASSWORD
+ 7 other calls in file
GitHub: pbxx/plugdb-postgres
30 31 32 33 34 35 36 37 38 39
...options, } //this.options = options //create this.pool this.pool = new Pool(options); //add CRUD and utility methods this.create = { schema: async (opts) => { var defaults = {
GitHub: pelnik/juicebox
0 1 2 3 4 5 6 7 8 9 10
require('dotenv').config(); const { Pool } = require('pg'); const { DATABASE_URL } = process.env; console.log(DATABASE_URL, 'DATABASE_URL'); let client = new Pool({ connectionString: DATABASE_URL, idleTimeoutMillis: 30000, }); // const client = new Client('postgres://localhost:5432/juicebox-dev');