{"id":13395690,"url":"https://github.com/xxgreg/dart_postgresql","last_synced_at":"2026-02-20T20:38:01.487Z","repository":{"id":7194484,"uuid":"8498401","full_name":"xxgreg/dart_postgresql","owner":"xxgreg","description":"Dart Postgresql database library.","archived":false,"fork":false,"pushed_at":"2016-10-29T21:29:43.000Z","size":564,"stargazers_count":85,"open_issues_count":19,"forks_count":29,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-10-23T08:22:40.805Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Dart","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xxgreg.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2013-03-01T09:42:22.000Z","updated_at":"2025-06-24T15:19:56.000Z","dependencies_parsed_at":"2022-08-19T02:12:21.191Z","dependency_job_id":null,"html_url":"https://github.com/xxgreg/dart_postgresql","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/xxgreg/dart_postgresql","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxgreg%2Fdart_postgresql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxgreg%2Fdart_postgresql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxgreg%2Fdart_postgresql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxgreg%2Fdart_postgresql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xxgreg","download_url":"https://codeload.github.com/xxgreg/dart_postgresql/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xxgreg%2Fdart_postgresql/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29604112,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-19T04:38:07.383Z","status":"ssl_error","status_checked_at":"2026-02-19T04:35:50.016Z","response_time":117,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2024-07-30T18:00:28.175Z","updated_at":"2026-02-20T20:38:01.465Z","avatar_url":"https://github.com/xxgreg.png","language":"Dart","readme":"# PostgreSQL database driver for Dart\n\n[![Build Status](https://travis-ci.org/xxgreg/dart_postgresql.svg?branch=master)](https://travis-ci.org/xxgreg/dart_postgresql/)\n\n## Basic usage\n\n### Obtaining a connection\n\n```dart\nvar uri = 'postgres://username:password@localhost:5432/database';\nconnect(uri).then((conn) {\n\t// ...\n});\n```\n\n### SSL connections\n\nSet the sslmode to require by appending this to the connection uri. This driver only supports sslmode=require, if sslmode is ommitted the driver will always connect without using SSL.\n\n```dart\nvar uri = 'postgres://username:password@localhost:5432/database?sslmode=require';\nconnect(uri).then((conn) {\n\t// ...\n});\n```\n\n### Querying\n\n```dart\nconn.query('select color from crayons').toList().then((rows) {\n\tfor (var row in rows) {\n\t\tprint(row.color); // Refer to columns by name,\n\t\tprint(row[0]);    // Or by column index.\n\t}\n});\n```\n\n### Executing\n\n```dart\nconn.execute(\"update crayons set color = 'pink'\").then((rowsAffected) {\n\tprint(rowsAffected);\n});\n```\n\n### Query Parameters\n\nQuery parameters can be provided using a map. Strings will be escaped to prevent SQL injection vulnerabilities.\n\n```dart\nconn.query('select color from crayons where id = @id', {'id': 5})\n  .toList()\n\t.then((result) { print(result); });\n\nconn.execute('insert into crayons values (@id, @color)',\n             {'id': 1, 'color': 'pink'})\n\t.then((_) { print('done.'); });\n```\n\n### Closing the connection\n\nYou must remember to call Connection.close() when you're done. This won't be\ndone automatically for you.\n\n### Conversion of Postgresql datatypes.\n\nBelow is the mapping from Postgresql types to Dart types. All types which do not have an explicit mapping will be returned as a String in Postgresql's standard text format. This means that it is still possible to handle all types, as you can parse the string yourself.\n\n```\n     Postgresql type                 Dart type\n\tboolean                         bool\n\tint2, int4, int8                int\n\tfloat4, float8                  double\n\tnumeric                         String\n\ttimestamp, timestamptz, date    Datetime\n\tjson, jsonb                     Map/List\n\tAll other types                 String\n```\n\n### Mapping the results of a query to an object\n\n```dart\nclass Crayon {\n\tString color;\n\tint length;\n}\n\nconn.query('select color, length from crayons')\n\t.map((row) =\u003e new Crayon()\n\t                     ..color = row.color\n\t                     ..length = row.length)\n\t.toList()\n\t.then((List\u003cCrayon\u003e crayons) {\n\t\tfor (var c in crayons) {\n\t\t\tprint(c is Crayon);\n\t\t\tprint(c.color);\n\t\t\tprint(c.length);\n\t\t}\n\t});\n```\n\nOr for an immutable object:\n\n```dart\nclass ImmutableCrayon {\n\tImmutableCrayon(this.color, this.length);\n\tfinal String color;\n\tfinal int length;\n}\n\nconn.query('select color, length from crayons')\n  .map((row) =\u003e new ImmutableCrayon(row.color, row.length))\n     .toList()\n\t.then((List\u003cImmutableCrayon\u003e crayons) {\n\t\tfor (var c in crayons) {\n\t\t\tprint(c is ImmutableCrayon);\n\t\t\tprint(c.color);\n\t\t\tprint(c.length);\n\t\t}\n\t});\n```\n\n### Query queueing\n\nQueries are queued and executed in the order in which they were queued.\n\nSo if you're not concerned about handling errors, you can write code like this:\n\n```dart\nconn.execute(\"create table crayons (color text, length int)\");\nconn.execute(\"insert into crayons values ('pink', 5)\");\nconn.query(\"select color from crayons\").single.then((crayon) {\n\tprint(crayon.color); // prints 'pink'\n});\n```\n\n### Query streaming\n\nConnection.query() returns a Stream of results. You can use each row as soon as\nit is received, or you can wait till they all arrive by calling Stream.toList().\n\n### Connection pooling\n\nIn server applications, a connection pool can be used to avoid the overhead of obtaining a connection for each request.\n\n```dart\nimport 'package:postgresql/pool.dart';\n\nmain() {\n  var uri = 'postgres://username:password@localhost:5432/database';\n  var pool = new Pool(uri, minConnections: 2, maxConnections: 5);\n  pool.messages.listen(print);\n  pool.start().then((_) {\n    print('Min connections established.');\n    pool.connect().then((conn) { // Obtain connection from pool\n      conn.query(\"select 'oi';\")\n        .toList()\n        .then(print)\n        .then((_) =\u003e conn.close()) // Return connection to pool\n        .catchError((err) =\u003e print('Query error: $err'));\n    });\n  });\n}\n```\n\n### Example program\n\nAdd postgresql to your pubspec.yaml file, and run pub install.\n\n```\nname: postgresql_example\ndependencies:\n  postgresql: any\n```\n\n```dart\nimport 'package:postgresql/postgresql.dart';\n\nvoid main() {\n  var uri = 'postgres://testdb:password@localhost:5432/testdb';\n  var sql = \"select 'oi'\"; \n  connect(uri).then((conn) {\n    conn.query(sql).toList()\n    \t.then((result) {\n    \t\tprint('result: $result');\n    \t})\n    \t.whenComplete(() {\n    \t\tconn.close();\n    \t});\n  });\n}\n```\n\n## Testing\n\nTo run the unit tests you will need to create a database, and edit\n'test/config.yaml' accordingly.\n\n### Creating a database for testing\n\nChange to the postgres user and run the administration commands.\n```bash\nsudo su postgres\ncreateuser --pwprompt testdb\n  Enter password for new role: password\n  Enter it again: password\n  Shall the new role be a superuser? (y/n) n\n  Shall the new role be allowed to create databases? (y/n) n\n  Shall the new role be allowed to create more new roles? (y/n) n\ncreatedb --owner testdb testdb\nexit\n```\n\nCheck that it worked by logging in.\n```bash\npsql -h localhost -U testdb -W\n```\n\nEnter \"\\q\" to quit from the psql console.\n\n## License\n\nBSD\n\n## Links\n\nhttp://www.postgresql.org/docs/9.2/static/index.html\nhttp://www.dartlang.org/\n","funding_links":[],"categories":["Database","数据库","Libraries"],"sub_categories":["Database"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxxgreg%2Fdart_postgresql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxxgreg%2Fdart_postgresql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxxgreg%2Fdart_postgresql/lists"}