{"id":42518,"library":"co-pg","title":"co-pg","description":"A wrapper for node-postgres (pg) that provides promise-based methods (e.g., connectPromise, queryPromise) compatible with the co generator-based control flow library and async/await. Current version 2.0.2, last updated in 2022. It allows using PostgreSQL connections and queries with yield or await instead of callbacks, without altering the original pg API. Supports pg, pg-native, and pg-pure backends. The library is in maintenance mode; newer projects should prefer native async/await with pg directly.","status":"maintenance","version":"2.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/basicdays/co-pg","tags":["javascript","async","generator","coroutine","co","postgresql","postgres","pg"],"install":[{"cmd":"npm install co-pg","lang":"bash","label":"npm"},{"cmd":"yarn add co-pg","lang":"bash","label":"yarn"},{"cmd":"pnpm add co-pg","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency: wraps pg API with promise helpers. Must be provided by user.","package":"pg","optional":false}],"imports":[{"note":"CommonJS only. The library must be called as a function with the pg module as argument. Not available as ESM import.","wrong":"const pg = require('co-pg');","symbol":"co-pg","correct":"const pg = require('co-pg')(require('pg'));"},{"note":"Access Client via the wrapped pg object, not destructured from the wrapper directly.","wrong":"const { Client } = require('co-pg')(require('pg'));","symbol":"Client","correct":"const client = new pg.Client(connectionString); yield client.connectPromise();"},{"note":"query is the original callback-based method; queryPromise returns a promise for use with co or async/await.","wrong":"yield client.query('SELECT NOW()');","symbol":"queryPromise","correct":"yield client.queryPromise('SELECT NOW()');"}],"quickstart":{"code":"const co = require('co');\nconst pg = require('co-pg')(require('pg'));\n\nco(function*() {\n  const client = new pg.Client(process.env.DATABASE_URL ?? 'postgres://postgres:1234@localhost/postgres');\n  yield client.connectPromise();\n  const result = yield client.queryPromise('SELECT NOW() as now');\n  console.log(result.rows[0].now);\n  client.end();\n}).catch(err => console.error(err));","lang":"javascript","description":"Demonstrates connecting to PostgreSQL using co-pg with co, querying, and closing connection."},"warnings":[{"fix":"Replace co-pg with native pg promises: const { Pool } = require('pg'); const pool = new Pool(); await pool.query(...);","message":"co-pg is not actively maintained; favors callback-based pg patterns converted to promises. For new projects, use pg's built-in async/await support with pg.Pool.","severity":"deprecated","affected_versions":">=2.0.0"},{"fix":"Upgrade to 2.0.0 or later, or use async/await directly with co-pg (though co itself is outdated).","message":"The library requires an older version of co (4.x) and may not work with modern async/await without co.","severity":"gotcha","affected_versions":"<2.0.0"},{"fix":"Always call the done function returned from connectPromise after finishing with the client.","message":"co-pg wraps pg, but does not automatically handle connection pooling; you must explicitly call done() after using a pooled client.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Use const pg = require('co-pg')(require('pg')); then access pg.Client.","message":"Version 2.0.0 changed the API to be a function that takes the pg module, instead of direct require. Old code like require('co-pg').Client will break.","severity":"breaking","affected_versions":">=2.0.0"}],"env_vars":null,"search_vec":"'2.0.2':34 '2022':38 'allow':40 'alter':54 'api':58 'async':85 'async/await':31,80 'await':49 'backend':68 'base':15,26 'callback':52 'co':2,23,88 'co-pg':1 'compat':20 'connect':43 'connectpromis':18 'control':27 'coroutin':87 'current':32 'direct':83 'e.g':17 'flow':28 'generat':25,86 'generator-bas':24 'instead':50 'javascript':84 'last':35 'librari':29,70 'mainten':73 'method':16 'mode':74 'nativ':63,79 'newer':75 'node':8 'node-postgr':7 'origin':56 'pg':3,10,57,60,62,66,82,91 'pg-nativ':61 'pg-pure':65 'postgr':9,90 'postgresql':42,89 'prefer':78 'project':76 'promis':14 'promise-bas':13 'provid':12 'pure':67 'queri':45 'querypromis':19 'support':59 'updat':36 'use':41 'version':33 'without':53 'wrapper':5 'yield':47","created_at":"2026-06-05T16:55:21.696504+00:00","updated_at":"2026-06-05T16:55:21.696504+00:00","problems":[{"fix":"Run 'npm install co-pg'","cause":"Package not installed or missing from package.json.","error":"Error: Cannot find module 'co-pg'"},{"fix":"Use const pg = require('co-pg')(require('pg'));","cause":"Calling require('co-pg') without passing the pg module; in v2, co-pg exports a function that must be called.","error":"TypeError: require(...) is not a function"},{"fix":"Ensure pg = require('co-pg')(require('pg')) and use new pg.Client(...).","cause":"Client was not created from the wrapped pg object, or the wrapper was not correctly initialized.","error":"TypeError: client.queryPromise is not a function"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/basicdays/co-pg","github":"https://github.com/basicdays/co-pg","docs":null,"changelog":null,"pypi":null,"npm":"co-pg","openapi_spec":null,"status_page":null,"smithery":null,"categories":["database"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-05","next_check":"2026-09-03","install_tag":null}}