{"id":20913799,"url":"https://github.com/deno-library/pg","last_synced_at":"2025-09-02T21:41:17.660Z","repository":{"id":62421456,"uuid":"271968042","full_name":"deno-library/pg","owner":"deno-library","description":"PostgreSQL client for Deno","archived":false,"fork":false,"pushed_at":"2021-10-14T05:43:40.000Z","size":38,"stargazers_count":8,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-19T21:48:38.707Z","etag":null,"topics":["deno","postgresql"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/deno-library.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-13T08:13:58.000Z","updated_at":"2022-10-21T14:41:46.000Z","dependencies_parsed_at":"2022-11-01T17:32:01.003Z","dependency_job_id":null,"html_url":"https://github.com/deno-library/pg","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deno-library%2Fpg","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deno-library%2Fpg/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deno-library%2Fpg/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/deno-library%2Fpg/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/deno-library","download_url":"https://codeload.github.com/deno-library/pg/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253913064,"owners_count":21983249,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["deno","postgresql"],"created_at":"2024-11-18T15:07:57.709Z","updated_at":"2025-05-13T09:31:21.661Z","avatar_url":"https://github.com/deno-library.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# deno-pg\nPostgreSQL client for Deno\n\n### Status\n* [x] client\n* [x] pool\n* [ ] ssl\n* [x] transactions\n\n## Useage  \n\n### Client \n```ts\nimport { Client, Pool } from \"https://deno.land/x/pg@v0.6.1/mod.ts\";\n\nconst client = new Client({\n  user: 'postgres',\n  hostname: '127.0.0.1',\n  database: 'test',\n  password: '123456',\n  port: 5432\n});\n\nawait client.connect();\n\nconst res = await client.query('SELECT * from users');\nconsole.log(res);\nawait client.end();\n```  \n\n### ssl \n```js\nimport { Client, Pool } from \"https://deno.land/x/pg@v0.6.1/mod.ts\";\n\nconst client = new Client({\n  user: 'postgres',\n  hostname: 'localhost',\n  database: 'test',\n  password: '123456',\n  port: 5432,\n  certFile: \"./certs/my_custom_root_CA.pem\"\n});\n\nawait client.connect();\n\nconst res = await client.query('SELECT * from users');\nconsole.log(res);\nawait client.end();\n```  \n\n### Pool\nThe client pool allows you to have a reusable pool of clients you can check out, use, and return. You generally want a limited number of these in your application and usually just 1. Creating an unbounded number of pools defeats the purpose of pooling at all.\n\n#### Checkout, use, and return\n```js\nimport {  Pool } from \"https://deno.land/x/pg@v0.6.1/mod.ts\";\n\nconst pool = new Pool({\n  user: \"postgres\",\n  hostname: \"127.0.0.1\",\n  database: \"test\",\n  password: \"123456\",\n  port: 5432,\n});\n\nconst client = await pool.connect();\ntry {\n  const res = await client.query(\"SELECT * from users\");\n  console.log(res);\n} catch (error) {\n  console.log(error);\n} finally {\n  client.release();\n}\n```\nYou must always return the client to the pool if you successfully check it out, regardless of whether or not there was an error with the queries you ran on the client. If you don't check in the client your application will leak them and eventually your pool will be empty forever and all future requests to check out a client from the pool will wait forever.\n\n#### Single query\nIf you don't need a transaction or you just need to run a single query, the pool has a convenience method to run a query on any available client in the pool. This is the preferred way to query with node-postgres if you can as it removes the risk of leaking a client.\n```js\nimport {  Pool } from \"https://deno.land/x/pg@v0.6.1/mod.ts\";\n\nconst pool = new Pool({\n  user: \"postgres\",\n  hostname: \"127.0.0.1\",\n  database: \"test\",\n  password: \"123456\",\n  port: 5432,\n});\n\nconst res = await pool.query(\"SELECT * from users\");\nconsole.log(res);\n```\n\n### Transation\nTo execute a transaction with node-postgres you simply execute BEGIN / COMMIT / ROLLBACK queries yourself through a client. Because node-postgres strives to be low level and un-opinionated, it doesn't provide any higher level abstractions specifically around transactions.\n\nYou must use the same client instance for all statements within a transaction. PostgreSQL isolates a transaction to individual clients. This means if you initialize or use transactions with the pool.query method you will have problems. Do not use transactions with the pool.query method.\n\n```js\nimport {  Pool } from \"https://deno.land/x/pg@v0.6.1/mod.ts\";\n\nconst pool = new Pool({\n  user: \"postgres\",\n  hostname: \"127.0.0.1\",\n  database: \"test\",\n  password: \"123456\",\n  port: 5432,\n});\n\n// note: we don't try/catch this because if connecting throws an exception\n// we don't need to dispose of the client (it will be undefined)\nconst client = await pool.connect();\ntry {\n  await client.query(\"BEGIN\");\n  await client.query(`INSERT INTO users(id, name) VALUES(1, 'zfx')`);\n  await client.query(`INSERT INTO users(id, name) VALUES(2, 'zfx2')`);\n  await client.query(`DELETE FROM users WHERE id=0`);\n  await client.query(\"COMMIT\");\n} catch (e) {\n  await client.query(\"ROLLBACK\");\n  throw e;\n} finally {\n  client.release();\n}\n```\n\n### Shutdown\nTo shut down a pool call pool.end() on the pool. This will wait for all checked-out clients to be returned and then shut down all the clients and the pool timers.\n```js\nimport { Pool } from \"https://deno.land/x/pg@v0.6.1/mod.ts\";\n\nconst pool = new Pool({\n  user: \"postgres\",\n  hostname: \"127.0.0.1\",\n  database: \"test\",\n  password: \"123456\",\n  port: 5432,\n});\n\nconst res = await pool.query(\"SELECT * from users\");\nconsole.log(res);\nawait pool.end();\n// will throw error\nawait pool.query(\"SELECT * from users\");\n```\nThe pool will return errors when attempting to check out a client after you've called `pool.end()` on the pool.\n\n## Interface  \n```ts\nexport interface ClientConfig {\n  user?: string;\n  password: string;\n  database?: string;\n  hostname?: string;\n  certFile?: string;\n  port?: number;\n}\n\ninterface PoolConfig extends ClientConfig {\n  // maximum number of clients the pool should contain\n  // by default this is set to 10.\n  max?: number;\n  // Determines the pool's action when no connections are available and the limit has \n  // been reached. If true, the pool will queue the connection request and call it \n  // when one becomes available. If false, the pool will immediately call back with \n  // an error. (Default: true)\n  waitForConnections?: boolean;\n  // number of milliseconds to wait before timing out when waiting for connection.\n  // by default this is 0 which means no timeout\n  waitForConnectionsMillis?: number;\n  // The maximum number of connection requests the pool will queue before returning \n  // an error from getConnection. If set to 0, there is no limit to the number of \n  // queued connection requests. (Default: 0)\n  queueLimit?: number;\n  // number of milliseconds a client must sit idle in the pool and not be checked out\n  // before it is disconnected from the backend and discarded\n  // default is 10000 (10 seconds) - set to 0 to disable auto-disconnection of idle clients\n  idleTimeoutMillis?: number;\n}\n```\n\n## Test\n\n```bash\n$ deno test --allow-net --allow-read\n```  \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeno-library%2Fpg","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdeno-library%2Fpg","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdeno-library%2Fpg/lists"}