{"id":20757218,"url":"https://github.com/vapor-community/postgresql","last_synced_at":"2025-05-11T07:31:04.285Z","repository":{"id":54150215,"uuid":"63276577","full_name":"vapor-community/postgresql","owner":"vapor-community","description":"Robust PostgreSQL interface for Swift","archived":true,"fork":false,"pushed_at":"2020-05-18T15:19:18.000Z","size":187,"stargazers_count":131,"open_issues_count":9,"forks_count":33,"subscribers_count":17,"default_branch":"master","last_synced_at":"2025-03-23T00:45:35.884Z","etag":null,"topics":["postgres","swift","vapor"],"latest_commit_sha":null,"homepage":null,"language":"Swift","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/vapor-community.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":"2016-07-13T20:17:43.000Z","updated_at":"2024-07-05T18:20:56.000Z","dependencies_parsed_at":"2022-11-29T13:20:26.011Z","dependency_job_id":null,"html_url":"https://github.com/vapor-community/postgresql","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vapor-community%2Fpostgresql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vapor-community%2Fpostgresql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vapor-community%2Fpostgresql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vapor-community%2Fpostgresql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vapor-community","download_url":"https://codeload.github.com/vapor-community/postgresql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253533227,"owners_count":21923383,"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":["postgres","swift","vapor"],"created_at":"2024-11-17T09:40:58.116Z","updated_at":"2025-05-11T07:31:03.995Z","avatar_url":"https://github.com/vapor-community.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Swift](https://img.shields.io/badge/swift-3.1_--_4.0-brightgreen.svg)](https://swift.org)\n[![Linux Build Status](https://img.shields.io/circleci/project/github/vapor-community/postgresql.svg?label=Linux)](https://circleci.com/gh/vapor-community/postgresql)\n[![macOS Build Status](https://img.shields.io/travis/vapor-community/postgresql.svg?label=macOS)](https://travis-ci.org/vapor-community/postgresql)\n[![codecov](https://codecov.io/gh/vapor-community/postgresql/branch/master/graph/badge.svg)](https://codecov.io/gh/vapor-community/postgresql)\n[![GitHub license](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE)\n\n# PostgreSQL for Swift\n\n## Prerequisites\n\nThe PostgreSQL C driver must be installed in order to use this package.  \nFollow the [README of the cpostgresql repo](https://github.com/vapor-community/cpostgresql/blob/master/README.md) to get started.\n\n## Using PostgreSQL\n\nThis section outlines how to import the PostgreSQL package both with or without a Vapor project.\n\n### With Vapor\n\nThe easiest way to use PostgreSQL with Vapor is to include the PostgreSQL provider.\n\n```swift\nimport PackageDescription\n\nlet package = Package(\n    name: \"Project\",\n    dependencies: [\n        .Package(url: \"https://github.com/vapor/vapor.git\", majorVersion: 2),\n        .Package(url: \"https://github.com/vapor-community/postgresql-provider.git\", majorVersion: 2)\n    ],\n    exclude: [ ... ]\n)\n```\n\nThe PostgreSQL provider package adds PostgreSQL to your project and adds some additional, Vapor-specific conveniences like `drop.postgresql()`.\n\nUsing `import PostgreSQLProvider` will import both Fluent and Fluent's Vapor-specific APIs.\n\n### With Fluent\n\nFluent is a powerful, pure-Swift ORM that can be used with any Server-Side Swift framework. The PostgreSQL driver allows you to use a PostgreSQL database to power your models and queries.\n\n```swift\nimport PackageDescription\n\nlet package = Package(\n    name: \"Project\",\n    dependencies: [\n        ...\n        .Package(url: \"https://github.com/vapor/fluent.git\", majorVersion: 2),\n        .Package(url: \"https://github.com/vapor-community/postgresql-driver.git\", majorVersion: 2)\n    ],\n    exclude: [ ... ]\n)\n```\n\nUse `import PostgreSQLDriver` to access the `PostgreSQLDriver` class which you can use to initialize a Fluent `Database`.\n\n### Just PostgreSQL\n\nAt the core of the PostgreSQL provider and PostgreSQL driver is a Swift wrapper around the C PostgreSQL client. This package can be used by itself to send raw, parameterized queries to your PostgreSQL database.\n\n```swift\nimport PackageDescription\n\nlet package = Package(\n    name: \"Project\",\n    dependencies: [\n        ...\n        .Package(url: \"https://github.com/vapor/postgresql.git\", majorVersion: 2)\n    ],\n    exclude: [ ... ]\n)\n```\n\nUse `import PostgreSQL` to access the `PostgreSQL.Database` class.\n\n\n# Examples\n\n## Connecting to the Database\n\n```swift\nimport PostgreSQL\n\nlet postgreSQL =  PostgreSQL.Database(\n    hostname: \"localhost\",\n    database: \"test\",\n    user: \"root\",\n    password: \"\"\n)\n```\n\n## Select\n\n```swift\nlet version = try postgreSQL.execute(\"SELECT version()\")\n```\n\n## Prepared Statement\n\nThe second parameter to `execute()` is an array of `PostgreSQL.Value`s.\n\n```swift\nlet results = try postgreSQL.execute(\"SELECT * FROM users WHERE age \u003e= $1\", [.int(21)])\n```\n\n## Listen and Notify\n\n```swift\ntry postgreSQL.listen(to: \"test_channel\") { notification in\n    print(notification.channel)\n    print(notification.payload)\n}\n\n// Allow set up time for LISTEN\nsleep(1)\n\ntry postgreSQL.notify(channel: \"test_channel\", payload: \"test_payload\")\n\n```\n\n## Connection\n\nEach call to `execute()` creates a new connection to the PostgreSQL database. This ensures thread safety since a single connection cannot be used on more than one thread.\n\nIf you would like to re-use a connection between calls to execute, create a reusable connection and pass it as the third parameter to `execute()`.\n\n```swift\nlet connection = try postgreSQL.makeConnection()\nlet result = try postgreSQL.execute(\"SELECT * FROM users WHERE age \u003e= $1\", [.int(21)]), connection)\n```\n\n## Contributors\n\nMaintained by [Steven Roebert](https://github.com/sroebert), [Nate Bird](https://twitter.com/natesbird), [Prince Ugwuh](https://twitter.com/Prince2k3), and other members of the Vapor community.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvapor-community%2Fpostgresql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvapor-community%2Fpostgresql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvapor-community%2Fpostgresql/lists"}