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: "https://github.com/stepanhruda/PostgreSQL-Swift.git", majorVersion: 0)Your database configuration should not be in your application's source. Configuration is automatically loaded from default PostgreSQL environment variables.
export PGHOST 123.123.123.123
export PGPORT 9000
export PGDATABASE banana_pantry
export PGUSER mehungry
export PGPASSWORD reallyhungrygotnopatienceIn 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 = database.open()
defer {
  database.close(connection)
}
// Use connection to execute querieslet 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)
}- Install dependencies
 
- Xcode 7+ (Swift 2.x)
 brew cask install dockertoolbox
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.setupalso 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.xcschemefile.
make testto run tests or run them through Xcode