Swift PostgreSQL driver


Connect to your PostgreSQL database. Run queries. All natively in Swift.


Install via swift-package-manager by adding a depdendency to your Package.swift.

.Package(url: "", majorVersion: 0)



Your database configuration should not be in your application's source. Configuration is automatically loaded from default PostgreSQL environment variables.

export PGHOST
export PGPORT 9000
export PGDATABASE banana_pantry
export PGUSER mehungry
export PGPASSWORD reallyhungrygotnopatience

In your application, create a single global database object. It's thread-safe and manages a pool of connections to save resources.

let database = Database()

Now anytime you want to use the database:

let connection =

defer {

// Use connection to execute queries

Queries and results

let result = try connection.execute("SELECT color, is_tasty, length FROM bananas WHERE source = $1;", parameters: [palmTree])
for row in result.rows {
  let color = row["color"] as! String
  let isTasty = row["is_tasty"] as! Bool
  let length = row["length"] as! Int
  let banana = Banana(color: color, isTasty: isTasty, length: length)

Development on OS X

  1. Install dependencies
    • Xcode 7+ (Swift 2.x)
    • brew cask install dockertoolbox
  2. make development.setup
    • Starts a PostgreSQL container that tests can be run against. Before running make sure your docker-machine environment variables are available (usually you run eval $(docker-machine env default))
    • development.setup also adds handy opinionated environment variables to your Xcode scheme that connect to the container. If you are using a custom setup rather than what docker-machine gives you out of the box, you might need to tweak them. Also, please don't commit any changes to the .xcscheme file.
  3. make test to run tests or run them through Xcode