https://github.com/vapor-community/postgresql-provider
PostgreSQL Provider for the Vapor web framework.
https://github.com/vapor-community/postgresql-provider
cache database postgresql swift vapor vapor-provider
Last synced: about 1 year ago
JSON representation
PostgreSQL Provider for the Vapor web framework.
- Host: GitHub
- URL: https://github.com/vapor-community/postgresql-provider
- Owner: vapor-community
- License: mit
- Created: 2016-07-18T15:09:10.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2018-01-16T17:27:39.000Z (over 8 years ago)
- Last Synced: 2025-04-29T08:55:44.008Z (about 1 year ago)
- Topics: cache, database, postgresql, swift, vapor, vapor-provider
- Language: Swift
- Size: 40 KB
- Stars: 69
- Watchers: 16
- Forks: 19
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://swift.org)
[](https://circleci.com/gh/vapor-community/postgresql-provider)
[](https://travis-ci.org/vapor-community/postgresql-provider)
[](https://codecov.io/gh/vapor-community/postgresql-provider)
[](LICENSE)
# PostgreSQL Provider for Vapor
Adds PostgreSQL support to the Vapor web framework.
## Prerequisites
The PostgreSQL C driver must be installed in order to use this package.
Follow the [README of the cpostgresql repo](https://github.com/vapor-community/cpostgresql/blob/master/README.md) to get started.
## Setup
Note that the process is different for Swift 3 and 4.
1. Add the dependency to project
- Swift 3: add to Package.swift package dependencies
```swift
.Package(url: "https://github.com/vapor-community/postgresql-provider.git", majorVersion: 2, minor: 1)
```
- Swift 4: add to Package.swift package _and target_ dependencies
```swift
.package(url: "https://github.com/vapor-community/postgresql-provider.git", .upToNextMajor(from: "2.1.0"))
// ...
.target(name: "App", dependencies: ["Vapor", "FluentProvider", "PostgreSQLProvider"], ...)
```
2. Fetch dependencies and regenerate the Xcode project
```bash
vapor update
```
## Usage
```swift
import Vapor
import PostgreSQLProvider
let config = try Config()
try config.addProvider(PostgreSQLProvider.Provider.self)
let drop = try Droplet(config)
```
## Configure Fluent
Once the provider is added to your Droplet, you can configure Fluent to use the PostgreSQL driver.
`Config/fluent.json`
```json
"driver": "postgresql"
```
## Configure PostgreSQL
### Basic
Here is an example of a simple PostgreSQL configuration file.
`Config/secrets/postgresql.json`
```json
{
"hostname": "127.0.0.1",
"user": "postgres",
"password": "hello",
"database": "test",
"port": 5432
}
```
Alternatively, you can set a url with the configuration parameters.
`Config/secrets/postgresql.json`
```json
{
"url": "psql://user:pass@hostname:5432/database"
}
```
### Read Replicas
Read replicas can be supplied by passing a single `master` hostname and an array of `readReplicas` hostnames.
`Config/secrets/postgresql.json`
```json
{
"master": "master.postgresql.foo.com",
"readReplicas": ["read01.postgresql.foo.com", "read02.postgresql.foo.com"],
"user": "postgres",
"password": "hello",
"database": "test",
"port": 5432
}
```
### Driver
You can get access to the PostgreSQL Driver on the droplet.
```swift
import Vapor
import PostgreSQLProvider
let postgresqlDriver = try drop.postgresql()
```