{"id":13388765,"url":"https://github.com/loopbackio/loopback-connector-postgresql","last_synced_at":"2025-05-15T00:06:39.967Z","repository":{"id":39590832,"uuid":"13695735","full_name":"loopbackio/loopback-connector-postgresql","owner":"loopbackio","description":"PostgreSQL connector for LoopBack.","archived":false,"fork":false,"pushed_at":"2025-05-08T07:41:37.000Z","size":1691,"stargazers_count":116,"open_issues_count":26,"forks_count":181,"subscribers_count":47,"default_branch":"master","last_synced_at":"2025-05-09T12:02:51.640Z","etag":null,"topics":["hacktoberfest","loopback","loopback4","nodejs","postgresql"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/loopbackio.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2013-10-19T04:55:47.000Z","updated_at":"2025-05-02T19:13:55.000Z","dependencies_parsed_at":"2023-12-01T20:29:20.563Z","dependency_job_id":"0d8c9afd-707f-405f-b40c-321f47e495cf","html_url":"https://github.com/loopbackio/loopback-connector-postgresql","commit_stats":{"total_commits":587,"total_committers":85,"mean_commits":6.905882352941177,"dds":0.6950596252129472,"last_synced_commit":"1a58f535c14d46efd346a0bab97e2bc56d49e116"},"previous_names":["strongloop/loopback-connector-postgresql"],"tags_count":98,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loopbackio%2Floopback-connector-postgresql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loopbackio%2Floopback-connector-postgresql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loopbackio%2Floopback-connector-postgresql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/loopbackio%2Floopback-connector-postgresql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/loopbackio","download_url":"https://codeload.github.com/loopbackio/loopback-connector-postgresql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254249198,"owners_count":22039029,"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":["hacktoberfest","loopback","loopback4","nodejs","postgresql"],"created_at":"2024-07-30T13:00:53.798Z","updated_at":"2025-05-15T00:06:39.918Z","avatar_url":"https://github.com/loopbackio.png","language":"JavaScript","readme":"# loopback-connector-postgresql\n\n[PostgreSQL](https://www.postgresql.org/), is a popular open-source object-relational database.\nThe `loopback-connector-postgresql` module is the PostgreSQL connector for the LoopBack framework.\n\n\u003cdiv class=\"gh-only\"\u003e\nThe PostgreSQL connector supports both LoopBack 3 and LoopBack 4. For more information, see\n\u003ca href=\"https://loopback.io/doc/en/lb4/PostgreSQL-connector.html\"\u003eLoopBack 4 documentation\u003c/a\u003e,\n\u003ca href=\"http://loopback.io/doc/en/lb3/PostgreSQL-connector.html\"\u003eLoopBack 3 documentation\u003c/a\u003e\nand \u003ca href=\"#module-long-term-support-policy\"\u003eModule Long Term Support Policy\u003c/a\u003e below.\n\u003cbr/\u003e\u003cbr/\u003e\nNOTE: The PostgreSQL connector requires PostgreSQL 8.x or 9.x.\n\u003c/div\u003e\n\n## Installation\n\nIn your application root directory, enter this command to install the connector:\n\n```shell\n$ npm install loopback-connector-postgresql --save\n```\n\nThis installs the module from npm and adds it as a dependency to the application's `package.json` file.\n\nIf you create a PostgreSQL data source using the data source generator as described below, you don't have to do this, since the generator will run `npm install` for you.\n\n## Creating a data source\n\nFor LoopBack 4 users, use the LoopBack 4 [Command-line interface](https://loopback.io/doc/en/lb4/Command-line-interface.html) to generate a DataSource with PostgreSQL connector to your LB4 application. Run [`lb4 datasource`](https://loopback.io/doc/en/lb4/DataSource-generator.html), it will prompt for configurations such as host, post, etc. that are required to connect to a PostgreSQL database.\n\nAfter setting it up, the configuration can be found under `src/datasources/\u003cDataSourceName\u003e.datasource.ts`, which would look like this:\n\n```ts\nconst config = {\n  name: 'db',\n  connector: 'postgresql',\n  url: '',\n  host:'localhost',\n  port: 5432,\n  user: 'user',\n  password: 'pass',\n  database: 'testdb',\n};\n```\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eFor LoopBack 3 users\u003c/strong\u003e\u003c/summary\u003e\n\nUse the [Data source generator](http://loopback.io/doc/en/lb3/Data-source-generator.html) to add a PostgreSQL data source to your application.\nThe generator will prompt for the database server hostname, port, and other settings\nrequired to connect to a PostgreSQL database. It will also run the `npm install` command above for you.\n\nThe entry in the application's `/server/datasources.json` will look like this:\n\n{% include code-caption.html content=\"/server/datasources.json\" %}\n\n```javascript\n\"mydb\": {\n  \"name\": \"mydb\",\n  \"connector\": \"postgresql\"\n  \"host\": \"mydbhost\",\n  \"port\": 5432,\n  \"url\": \"postgres://admin:admin@mydbhost:5432/db1?ssl=false\",\n  \"database\": \"db1\",\n  \"password\": \"admin\",\n  \"user\": \"admin\",\n  \"ssl\": false\n}\n```\n\nEdit `datasources.json` to add other properties that enable you to connect the data source to a PostgreSQL database.\n\n\u003c/details\u003e\n\n### Connection Pool Settings\n\nYou can also specify connection pool settings in `\u003cDataSourceName\u003e.datasource.ts` ( or `datasources.json` for LB3 users). For instance you can specify the minimum and the maximum pool size, and the maximum pool client's idle time before closing the client.\n\nExample of `db.datasource.ts`:\n\n```ts\nconst config = {\n  name: 'db',\n  connector: 'postgresql',\n  url: '',\n  host: 'localhost',\n  port: 5432,\n  user: 'user',\n  password: 'pass',\n  database: 'testdb',\n  min: 5,\n  max: 200,\n  idleTimeoutMillis: 60000,\n  ssl: false\n};\n```\n\nCheck out [node-pg-pool](https://github.com/brianc/node-pg-pool) and [node postgres pooling example](https://github.com/brianc/node-postgres#pooling-example) for more information.\n\n### Configuration options\n\n\u003ctable\u003e\n  \u003cthead\u003e\n    \u003ctr\u003e\n      \u003cth\u003eProperty\u003c/th\u003e\n      \u003cth\u003eType\u003c/th\u003e\n      \u003cth\u003eDescription\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n    \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd\u003econnector\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003e\n        Connector name, either \"loopback-connector-postgresql\" or \"postgresql\"\n      \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003edatabase\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003eDatabase name\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003edebug\u003c/td\u003e\n      \u003ctd\u003eBoolean\u003c/td\u003e\n      \u003ctd\u003eIf true, turn on verbose mode to debug database queries and lifecycle.\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003ehost\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003eDatabase host name\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003epassword\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003ePassword to connect to database\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eport\u003c/td\u003e\n      \u003ctd\u003eNumber\u003c/td\u003e\n      \u003ctd\u003eDatabase TCP port\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eurl\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003eUse instead of the\u003ccode\u003ehost\u003c/code\u003e,\u003ccode\u003eport\u003c/code\u003e,\u003ccode\u003euser\u003c/code\u003e,\u003ccode\u003epassword\u003c/code\u003e,\n        and\u003ccode\u003edatabase\u003c/code\u003eproperties. For example:'postgres://test:mypassword@localhost:5432/dev'.\n      \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eusername\u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n      \u003ctd\u003eUsername to connect to database\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003emin\u003c/td\u003e\n      \u003ctd\u003eInteger\u003c/td\u003e\n      \u003ctd\u003eMinimum number of clients in the connection pool\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003emax\u003c/td\u003e\n      \u003ctd\u003eInteger\u003c/td\u003e\n      \u003ctd\u003eMaximum number of clients in the connection pool\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eidleTimeoutMillis\u003c/td\u003e\n      \u003ctd\u003eInteger\u003c/td\u003e\n      \u003ctd\u003eMaximum time a client in the pool has to stay idle before closing it\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003essl\u003c/td\u003e\n      \u003ctd\u003eBoolean\u003c/td\u003e\n      \u003ctd\u003eWhether to try SSL/TLS to connect to server\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003edefaultIdSort\u003c/td\u003e\n      \u003ctd\u003eBoolean/String\u003c/td\u003e\n      \u003ctd\u003eSet to \u003ccode\u003efalse\u003c/code\u003e to disable default sorting on \u003ccode\u003eid\u003c/code\u003e column(s). Set to \u003ccode\u003enumericIdOnly\u003c/code\u003e to only apply to IDs with a number type \u003ccode\u003eid\u003c/code\u003e.\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eallowExtendedOperators\u003c/td\u003e\n      \u003ctd\u003eBoolean\u003c/td\u003e\n      \u003ctd\u003eSet to \u003ccode\u003etrue\u003c/code\u003e to enable PostgreSQL-specific operators\n          such as \u003ccode\u003econtains\u003c/code\u003e. Learn more in\n          \u003ca href=\"#extended-operators\"\u003eExtended operators\u003c/a\u003e below.\n      \u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\n**NOTE**: By default, the 'public' schema is used for all tables.\n\nThe PostgreSQL connector uses [node-postgres](https://github.com/brianc/node-postgres) as the driver. For more\ninformation about configuration parameters, see [node-postgres documentation](https://github.com/brianc/node-postgres/wiki/Client#constructors).\n\n### Connecting to UNIX domain socket\n\nA common PostgreSQL configuration is to connect to the UNIX domain socket `/var/run/postgresql/.s.PGSQL.5432` instead of using the TCP/IP port. For example:\n\n```ts\nconst config = {\n  name: 'db',\n  connector: 'postgresql',\n  url: '',\n  host: '/var/run/postgresql/',\n  port: 5432,\n  user: 'user',\n  password: 'pass',\n  database: 'testdb',\n  debug: true\n};\n```\n\n## Defining models\n\nLoopBack allows you to specify some database settings through the model definition and/or the property definition. These definitions would be mapped to the database. Please check out the CLI [`lb4 model`](https://loopback.io/doc/en/lb4/Model-generator.html) for generating LB4 models. The following is a typical LoopBack 4 model that specifies the schema, table and column details through model definition and property definitions:\n\n```ts\n@model({\n  settings: { postgresql: { schema: 'public', table: 'inventory'} },\n})\nexport class Inventory extends Entity {\n  @property({\n    type: 'number',\n    required: true,\n    scale: 0,\n    id: 1,\n    postgresql: {\n      columnName: 'id',\n      dataType: 'integer',\n      dataLength: null,\n      dataPrecision: null,\n      dataScale: 0,\n      nullable: 'NO',\n    },\n  })\n  id: number;\n\n  @property({\n    type: 'string',\n    postgresql: {\n      columnName: 'name',\n      dataType: 'text',\n      dataLength: null,\n      dataPrecision: null,\n      dataScale: null,\n      nullable: 'YES',\n    },\n  })\n  name?: string;\n\n  @property({\n    type: 'boolean',\n    required: true,\n    postgresql: {\n      columnName: 'available',\n      dataType: 'boolean',\n      dataLength: null,\n      dataPrecision: null,\n      dataScale: null,\n      nullable: 'NO',\n    },\n  })\n  available: boolean;\n\n  constructor(data?: Partial\u003cUser\u003e) {\n    super(data);\n  }\n}\n```\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eFor LoopBack 3 users\u003c/strong\u003e\u003c/summary\u003e\n\nThe model definition consists of the following properties.\n\n\u003ctable\u003e\n  \u003cthead\u003e\n    \u003ctr\u003e\n      \u003cth\u003eProperty\u003c/th\u003e\n      \u003cth\u003eDefault\u003c/th\u003e\n      \u003cth\u003eDescription\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd\u003ename\u003c/td\u003e\n      \u003ctd\u003eCamel-case of the database table name\u003c/td\u003e\n      \u003ctd\u003eName of the model.\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eoptions\u003c/td\u003e\n      \u003ctd\u003eN/A\u003c/td\u003e\n      \u003ctd\u003eModel level operations and mapping to PostgreSQL schema/table\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eproperties\u003c/td\u003e\n      \u003ctd\u003eN/A\u003c/td\u003e\n      \u003ctd\u003eProperty definitions, including mapping to PostgreSQL column\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\nFor example:\n\n{% include code-caption.html content=\"/common/models/model.json\" %}\n\n```javascript\n{\n  \"name\": \"Inventory\",\n  \"options\": {\n    \"idInjection\": false,\n    \"postgresql\": {\n      \"schema\": \"strongloop\",\n      \"table\": \"inventory\"\n    }\n  },\n  \"properties\": {\n    \"id\": {\n      \"type\": \"String\",\n      \"required\": false,\n      \"length\": 64,\n      \"precision\": null,\n      \"scale\": null,\n      \"postgresql\": {\n        \"columnName\": \"id\",\n        \"dataType\": \"character varying\",\n        \"dataLength\": 64,\n        \"dataPrecision\": null,\n        \"dataScale\": null,\n        \"nullable\": \"NO\"\n      }\n    },\n    \"productId\": {\n      \"type\": \"String\",\n      \"required\": false,\n      \"length\": 20,\n      \"precision\": null,\n      \"scale\": null,\n      \"id\": 1,\n      \"postgresql\": {\n        \"columnName\": \"product_id\",\n        \"dataType\": \"character varying\",\n        \"dataLength\": 20,\n        \"dataPrecision\": null,\n        \"dataScale\": null,\n        \"nullable\": \"YES\"\n      }\n    },\n    \"locationId\": {\n      \"type\": \"String\",\n      \"required\": false,\n      \"length\": 20,\n      \"precision\": null,\n      \"scale\": null,\n      \"id\": 1,\n      \"postgresql\": {\n        \"columnName\": \"location_id\",\n        \"dataType\": \"character varying\",\n        \"dataLength\": 20,\n        \"dataPrecision\": null,\n        \"dataScale\": null,\n        \"nullable\": \"YES\"\n      }\n    },\n    \"available\": {\n      \"type\": \"Number\",\n      \"required\": false,\n      \"length\": null,\n      \"precision\": 32,\n      \"scale\": 0,\n      \"postgresql\": {\n        \"columnName\": \"available\",\n        \"dataType\": \"integer\",\n        \"dataLength\": null,\n        \"dataPrecision\": 32,\n        \"dataScale\": 0,\n        \"nullable\": \"YES\"\n      }\n    },\n    \"total\": {\n      \"type\": \"Number\",\n      \"required\": false,\n      \"length\": null,\n      \"precision\": 32,\n      \"scale\": 0,\n      \"postgresql\": {\n        \"columnName\": \"total\",\n        \"dataType\": \"integer\",\n        \"dataLength\": null,\n        \"dataPrecision\": 32,\n        \"dataScale\": 0,\n        \"nullable\": \"YES\"\n      }\n    }\n  }\n}\n```\n\n\u003c/details\u003e\n\nTo learn more about specifying database settings, please check the section [Data Mapping Properties](https://loopback.io/doc/en/lb4/Model.html#data-mapping-properties).\n\n## Type mapping\n\nSee [LoopBack 4 types](http://loopback.io/doc/en/lb4/LoopBack-types.html) (or [LoopBack 3 types](http://loopback.io/doc/en/lb3/LoopBack-types.html)) for\ndetails on LoopBack's data types.\n\n### LoopBack to PostgreSQL types\n\n\u003ctable\u003e\n  \u003cthead\u003e\n    \u003ctr\u003e\n      \u003cth width=\"450\"\u003eLoopBack Type\u003c/th\u003e\n      \u003cth width=\"450\"\u003ePostgreSQL Type\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eString\u003cbr\u003eJSON\u003cbr\u003eText\u003cbr\u003eDefault\u003c/td\u003e\n      \u003ctd\u003e\n        VARCHAR2\u003cbr/\u003e\n        Default length is 1024\n      \u003c/td\u003e\n    \u003c/tr\u003e\n     \u003ctr\u003e\n      \u003ctd\u003eString[]\u003c/td\u003e\n      \u003ctd\u003e\n        VARCHAR2[]\n      \u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eNumber\u003c/td\u003e\n      \u003ctd\u003eINTEGER\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eDate\u003c/td\u003e\n      \u003ctd\u003eTIMESTAMP WITH TIME ZONE\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eBoolean\u003c/td\u003e\n      \u003ctd\u003eBOOLEAN\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\nBesides the basic LoopBack types, as we introduced above, you can also specify the database type for model properties. It would be mapped to the database (see [Data Mapping Properties](https://loopback.io/doc/en/lb4/Model.html#data-mapping-properties)). For example, we would like the property `price` to have database type `double precision` in the corresponding table in the database, we have specify it as following:\n\n```ts\n  @property({\n    type: 'number',\n    postgresql: {\n      dataType: 'double precision',\n    },\n  })\n  price?: number;\n```\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eFor LoopBack 3 users\u003c/strong\u003e\u003c/summary\u003e\n\n```javascript\n\"properties\": {\n    // ..\n    \"price\": {\n      \"type\": \"Number\",\n      \"postgresql\": {\n        \"dataType\": \"double precision\",\n      }\n    },\n```\n\n\u003c/details\u003e\n\n{% include warning.html content=\"\nNot all database types are supported for operating CRUD operations and queries with filters. For example, type Array cannot be filtered correctly, see GitHub issues: [# 441](https://github.com/loopbackio/loopback-connector-postgresql/issues/441) and [# 342](https://github.com/loopbackio/loopback-connector-postgresql/issues/342).\n\" %}\n\n### PostgreSQL types to LoopBack\n\n\u003ctable\u003e\n  \u003cthead\u003e\n    \u003ctr\u003e\n      \u003cth width=\"450\"\u003ePostgreSQL Type\u003c/th\u003e\n      \u003cth width=\"450\"\u003eLoopBack Type\u003c/th\u003e\n    \u003c/tr\u003e\n  \u003c/thead\u003e\n  \u003ctbody\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eBOOLEAN\u003c/td\u003e\n      \u003ctd\u003eBoolean\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003e\n        VARCHAR\u003cbr\u003eCHARACTER VARYING\u003cbr\u003eCHARACTER\u003cbr\u003eCHAR\u003cbr\u003eTEXT\n      \u003c/td\u003e\n      \u003ctd\u003eString\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eBYTEA\u003c/td\u003e\n      \u003ctd\u003eNode.js \u003ca href=\"http://nodejs.org/api/buffer.html\"\u003eBuffer object\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eSMALLINT\u003cbr\u003eINTEGER\u003cbr\u003eBIGINT\u003cbr\u003eDECIMAL\u003cbr\u003eNUMERIC\u003cbr\u003eREAL\u003cbr\u003eDOUBLE PRECISION\u003cbr\u003eFLOAT\u003cbr\u003eSERIAL\u003cbr\u003eBIGSERIAL\u003c/td\u003e\n      \u003ctd\u003eNumber\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003eDATE\u003cbr\u003eTIMESTAMP\u003cbr\u003eTIMESTAMP WITH TIME ZONE\u003cbr\u003eTIMESTAMP WITHOUT TIME ZONE\u003cbr\u003eTIME\u003cbr\u003eTIME WITH TIME ZONE\u003cbr\u003eTIME WITHOUT TIME ZONE\u003c/td\u003e\n      \u003ctd\u003eDate\u003c/td\u003e\n    \u003c/tr\u003e\n    \u003ctr\u003e\n      \u003ctd\u003ePOINT\u003c/td\u003e\n      \u003ctd\u003e\u003ca href=\"http://apidocs.strongloop.com/loopback-datasource-juggler/#geopoint\"\u003eGeoPoint\u003c/a\u003e\u003c/td\u003e\n    \u003c/tr\u003e\n  \u003c/tbody\u003e\n\u003c/table\u003e\n\n## Numeric Data Type\n\n**Note**: The [node.js driver for postgres](https://github.com/brianc/node-postgres) by default casts `Numeric` type as a string on `GET` operation. This is to avoid _data precision loss_ since `Numeric` types in postgres cannot be safely converted to JavaScript `Number`.\n\nFor details, see the corresponding [driver issue](https://github.com/brianc/node-pg-types/issues/28).\n\n## Querying JSON fields\n\n**Note** The fields you are querying should be setup to use the JSON postgresql data type - see Defining models\n\nAssuming a model such as this:\n\n```ts\n  @property({\n    type: 'number',\n    postgresql: {\n      dataType: 'double precision',\n    },\n  })\n  price?: number;\n```\n\nYou can query the nested fields with dot notation:\n\n```ts\nCustomerRepository.find({\n  where: {\n    address.state: 'California',\n  },\n  order: 'address.city',\n});\n```\n\n## Extended operators\n\nPostgreSQL supports the following PostgreSQL-specific operators:\n\n- [`contains`](#operator-contains)\n- [`containedBy`](#operator-containedby)\n- [`containsAny`](#operator-containsany)\n- [`match`](#operator-match)\n\nPlease note extended operators are disabled by default, you must enable\nthem at datasource level or model level by setting `allowExtendedOperators` to\n`true`.\n\n### Operator `contains`\n\nThe `contains` operator allow you to query array properties and pick only\nrows where the stored value contains all of the items specified by the query.\n\nThe operator is implemented using PostgreSQL [array operator\n`@\u003e`](https://www.postgresql.org/docs/current/functions-array.html).\n\n**Note** The fields you are querying must be setup to use the postgresql array data type - see [Defining models](#defining-models) above.\n\nAssuming a model such as this:\n\n```ts\n@model({\n  settings: {\n    allowExtendedOperators: true,\n  }\n})\nclass Post {\n  @property({\n    type: ['string'],\n    postgresql: {\n      dataType: 'varchar[]',\n    },\n  })\n  categories?: string[];\n}\n```\n\nYou can query the tags fields as follows:\n\n```ts\nconst posts = await postRepository.find({\n  where: {\n    {\n      categories: {'contains': ['AA']},\n    }\n  }\n});\n```\n\n### Operator `containedBy`\n\nInverse of the `contains` operator, the `containedBy` operator allow you to query array properties and pick only\nrows where the all the items in the stored value are contained by the query.\n\nThe operator is implemented using PostgreSQL [array operator\n`\u003c@`](https://www.postgresql.org/docs/current/functions-array.html).\n\n**Note** The fields you are querying must be setup to use the postgresql array data type - see [Defining models](#defining-models) above.\n\nAssuming a model such as this:\n\n```ts\n@model({\n  settings: {\n    allowExtendedOperators: true,\n  }\n})\nclass Post {\n  @property({\n    type: ['string'],\n    postgresql: {\n      dataType: 'varchar[]',\n    },\n  })\n  categories?: string[];\n}\n```\n\nYou can query the tags fields as follows:\n\n```ts\nconst posts = await postRepository.find({\n  where: {\n    {\n      categories: {'containedBy': ['AA']},\n    }\n  }\n});\n```\n\n### Operator `containsAnyOf`\n\nThe `containsAnyOf` operator allow you to query array properties and pick only\nrows where the any of the items in the stored value matches any of the items in the query.\n\nThe operator is implemented using PostgreSQL [array overlap operator\n`\u0026\u0026`](https://www.postgresql.org/docs/current/functions-array.html).\n\n**Note** The fields you are querying must be setup to use the postgresql array data type - see [Defining models](#defining-models) above.\n\nAssuming a model such as this:\n\n```ts\n@model({\n  settings: {\n    allowExtendedOperators: true,\n  }\n})\nclass Post {\n  @property({\n    type: ['string'],\n    postgresql: {\n      dataType: 'varchar[]',\n    },\n  })\n  categories?: string[];\n}\n```\n\nYou can query the tags fields as follows:\n\n```ts\nconst posts = await postRepository.find({\n  where: {\n    {\n      categories: {'containsAnyOf': ['AA']},\n    }\n  }\n});\n```\n\n### Operator `match`\n\nThe `match` operator allows you to perform a [full text search using the `@@` operator](https://www.postgresql.org/docs/10/textsearch-tables.html#TEXTSEARCH-TABLES-SEARCH) in PostgreSQL.\n\nAssuming a model such as this:\n```ts\n@model({\n  settings: {\n    allowExtendedOperators: true,\n  }\n})\nclass Post {\n  @property({\n    type: 'string',\n  })\n  content: string;\n}\n```\nYou can query the content field as follows:\n\n```ts\nconst posts = await postRepository.find({\n  where: {\n    {\n      content: {match: 'someString'},\n    }\n  }\n});\n```\n\n## Discovery and auto-migration\n\n### Model discovery\n\nThe PostgreSQL connector supports _model discovery_ that enables you to create LoopBack models\nbased on an existing database schema. Once you defined your datasource:\n-  LoopBack 4 users could use the commend [`lb4 discover`](https://loopback.io/doc/en/lb4/Discovering-models.html) to discover models.\n- For LB3 users, please check [Discovering models from relational databases](https://loopback.io/doc/en/lb3/Discovering-models-from-relational-databases.html).\n\n(See [database discovery API](http://apidocs.strongloop.com/loopback-datasource-juggler/#datasource-prototype-discoverandbuildmodels) for related APIs information)\n\n### Auto-migration\n\nThe PostgreSQL connector also supports _auto-migration_ that enables you to create a database schema\nfrom LoopBack models.\n\nFor example, based on the following model, the auto-migration method would create/alter existing `customer` table under `public` schema in the database. Table `customer` would have two columns: `name` and `id`, where `id` is also the primary key and has the default value `SERIAL` as it has definition of `type: 'Number'` and `generated: true`:\n\n```ts\n@model()\nexport class Customer extends Entity {\n  @property({\n    id: true,\n    type: 'Number',\n    generated: true\n  })\n  id: number;\n\n  @property({\n    type: 'string'\n  })\n  name: string;\n}\n```\n\nBy default, tables generated by the auto-migration are under `public` schema and named in lowercase.\n\nBesides the basic model metadata, LoopBack allows you to specify part of the database schema definition via the\nproperty definition, which would be mapped to the database.\n\nFor example, based on the following model, after running the auto-migration script, a table named `CUSTOMER` under schema `market` will be created. Moreover, you can also have different names for your property and the corresponding column. In the example, by specifying the column name, the property `name` will be mapped to the `customer_name` column. This is useful when your database has a different naming convention than LoopBack (camelCase).\n\n```ts\n@model(\n  settings: {\n    postgresql: {schema: 'market', table: 'CUSTOMER'},\n  }\n)\nexport class Customer extends Entity {\n  @property({\n    id: true,\n    type: 'Number',\n    generated: true\n  })\n  id: number;\n\n  @property({\n    type: 'string',\n    postgresql: {\n      columnName: 'customer_name'\n    }\n  })\n  name: string;\n}\n```\n\nFor how to run the script and more details:\n- For LB4 users, please check [Database Migration](https://loopback.io/doc/en/lb4/Database-migrations.html)\n- For LB3 users, please check [Creating a database schema from models](https://loopback.io/doc/en/lb3/Creating-a-database-schema-from-models.html)\n\n(See [LoopBack auto-migrate method](http://apidocs.strongloop.com/loopback-datasource-juggler/#datasource-prototype-automigrate) for related APIs information)\n\nHere are some limitations and tips:\n\n- If you defined `generated: true` in the id property, it generates integers by default. For auto-generated uuid, see [Auto-generated id property](#auto-generated-id-property)\n- Only the id property supports the auto-generation setting `generated: true` for now\n- Auto-migration doesn't create foreign key constraints by default. But they can be defined through the model definition. See [Auto-migrate with foreign keys](#auto-migrateauto-update-models-with-foreign-keys)\n- Destroying models may result in errors due to foreign key integrity. First delete any related models by calling delete on models with relationships.\n\n### Auto-migrate/Auto-update models with foreign keys\n\nForeign key constraints can be defined in the model definition.\n\n**Note**: The order of table creation is important. A referenced table must exist before creating a foreign key constraint.\n\nDefine your models and the foreign key constraints as follows:\n\n`customer.model.ts`:\n\n```ts\n@model()\nexport class Customer extends Entity {\n  @property({\n    id: true,\n    type: 'Number',\n    generated: true\n  })\n  id: number;\n\n  @property({\n    type: 'string'\n  })\n  name: string;\n}\n```\n\n`order.model.ts`:\n\n```ts\n@model({\n  settings: {\n    foreignKeys: {\n      fk_order_customerId: {\n        name: 'fk_order_customerId',\n        entity: 'Customer',\n        entityKey: 'id',\n        foreignKey: 'customerId',\n        onDelete: 'CASCADE',\n        onUpdate: 'SET NULL'\n      },\n    },\n  })\nexport class Order extends Entity {\n  @property({\n    id: true,\n    type: 'Number',\n    generated: true\n  })\n  id: number;\n\n  @property({\n    type: 'string'\n  })\n  name: string;\n\n  @property({\n    type: 'Number'\n  })\n  customerId: number;\n}\n```\n\n\u003cdetails\u003e\u003csummary markdown=\"span\"\u003e\u003cstrong\u003eFor LoopBack 3 users\u003c/strong\u003e\u003c/summary\u003e\n\n```json\n({\n  \"name\": \"Customer\",\n  \"options\": {\n    \"idInjection\": false\n  },\n  \"properties\": {\n    \"id\": {\n      \"type\": \"Number\",\n      \"id\": 1\n    },\n    \"name\": {\n      \"type\": \"String\",\n      \"required\": false\n    }\n  }\n},\n{\n  \"name\": \"Order\",\n  \"options\": {\n    \"idInjection\": false,\n    \"foreignKeys\": {\n      \"fk_order_customerId\": {\n        \"name\": \"fk_order_customerId\",\n        \"entity\": \"Customer\",\n        \"entityKey\": \"id\",\n        \"foreignKey\": \"customerId\",\n        \"onDelete\": \"CASCADE\",\n        \"onUpdate\": \"SET NULL\"\n      }\n    }\n  },\n  \"properties\": {\n    \"id\": {\n      \"type\": \"Number\"\n      \"id\": 1\n    },\n    \"customerId\": {\n      \"type\": \"Number\"\n    },\n    \"description\": {\n      \"type\": \"String\",\n      \"required\": false\n    }\n  }\n})\n```\n\n\u003c/details\u003e\n\u003cbr\u003e\n{% include tip.html content=\"\nRemoving or updating the value of `foreignKeys` will be updated or delete or update the constraints in the db tables. If there is a reference to an object being deleted then the `DELETE` will fail. Likewise if there is a create with an invalid FK id then the `POST` will fail. The `onDelete` and `onUpdate` properties are optional and will default to `NO ACTION`.\n\" %}\n\n### Auto-generated ids\n\nAuto-migrate supports the automatic generation of property values for the id property. For PostgreSQL, the default id type is _integer_. Thus if you have `generated: true` in the id property, it generates integers by default:\n\n```ts\n{\n  id: true,\n  type: 'Number',\n  required: false,\n  generated: true // enables auto-generation\n}\n```\n\nIt is common to use UUIDs as the primary key in PostgreSQL instead of integers. You can enable it with either the following ways:\n\n- use uuid that is **generated by your LB application** by setting [`defaultFn: uuid`](https://loopback.io/doc/en/lb4/Model.html#property-decorator):\n\n\n```ts\n  @property({\n    id: true,\n    type: 'string'\n    defaultFn: 'uuid',\n    // generated: true,  -\u003e not needed\n  })\n  id: string;\n```\n\n- use PostgreSQL built-in (extension and) uuid functions:\n\n```ts\n  @property({\n  id: true,\n  type: 'String',\n  required: false,\n  // settings below are needed\n  generated: true,\n  useDefaultIdType: false,\n  postgresql: {\n    dataType: 'uuid',\n  },\n})\n  id: string;\n```\n\nThe setting uses `uuid-ossp` extension and `uuid_generate_v4()` function as default.\n\nIf you'd like to use other extensions and functions, you can do:\n\n```ts\n  @property({\n  id: true,\n  type: 'String',\n  required: false,\n  // settings below are needed\n  generated: true,\n  useDefaultIdType: false,\n  postgresql: {\n    dataType: 'uuid',\n    extension: 'myExtension',\n    defaultFn: 'myuuid'\n  },\n})\n  id: string;\n```\n\nWARNING: It is the users' responsibility to make sure the provided extension and function are valid.\n\n## Module Long Term Support Policy\n\nThis module adopts the [Module Long Term Support (LTS)](http://github.com/CloudNativeJS/ModuleLTS) policy, with the following End Of Life (EOL) dates:\n\n| Version | Status     | Published | EOL                  |\n| ------- | ---------- | --------- | -------------------- |\n| 5.x     | Current    | Apr 2020  | Apr 2023 _(minimum)_ |\n| 3.x     | Active LTS | Mar 2017  | Apr 2022             |\n\nLearn more about our LTS plan in [docs](https://loopback.io/doc/en/contrib/Long-term-support.html).\n\n## Running tests\n\n### Own instance\n\nIf you have a local or remote PostgreSQL instance and would like to use that to run the test suite, use the following command:\n\n- Linux\n\n```bash\nPOSTGRESQL_HOST=\u003cHOST\u003e POSTGRESQL_PORT=\u003cPORT\u003e POSTGRESQL_USER=\u003cUSER\u003e POSTGRESQL_PASSWORD=\u003cPASSWORD\u003e POSTGRESQL_DATABASE=\u003cDATABASE\u003e CI=true npm test\n```\n\n- Windows\n\n```bash\nSET POSTGRESQL_HOST=\u003cHOST\u003e SET POSTGRESQL_PORT=\u003cPORT\u003e SET POSTGRESQL_USER=\u003cUSER\u003e SET POSTGRESQL_PASSWORD=\u003cPASSWORD\u003e SET POSTGRESQL_DATABASE=\u003cDATABASE\u003e SET CI=true npm test\n```\n\n### Docker\n\nIf you do not have a local PostgreSQL instance, you can also run the test suite with very minimal requirements.\n\n- Assuming you have [Docker](https://docs.docker.com/engine/installation/) installed, run the following script which would spawn a PostgreSQL instance on your local:\n\n```bash\nsource setup.sh \u003cHOST\u003e \u003cPORT\u003e \u003cUSER\u003e \u003cPASSWORD\u003e \u003cDATABASE\u003e\n```\n\nwhere `\u003cHOST\u003e`, `\u003cPORT\u003e`, `\u003cUSER\u003e`, `\u003cPASSWORD\u003e` and `\u003cDATABASE\u003e` are optional parameters. The default values are `localhost`, `5432`, `root`, `pass` and `testdb` respectively.\n\n- Run the test:\n\n```bash\nnpm test\n```\n","funding_links":[],"categories":["Connectors","JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floopbackio%2Floopback-connector-postgresql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Floopbackio%2Floopback-connector-postgresql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Floopbackio%2Floopback-connector-postgresql/lists"}