To use Swift-Kuery-PostgreSQL you need to have the appropriate PostgreSQL C-language client installed.
On macOS we can use Homebrew to install Postgres:
brew install postgresql
On Linux we can use
apt to install PostgreSQL:
sudo apt install postgresql postgresql-contrib
Linux requires that you create a PostgreSQL user.
Now that we have PostgreSQL installed we can create a database:
Then we can open the PostgreSQL command-line interface:
Finally we can create a table in our database:
CREATE TABLE "BookTable" ( id varchar(100) PRIMARY KEY, title text NOT NULL, price float8 NOT NULL, genre text NOT NULL );
\q in terminal to exit psql.
Now we're ready to connect to our database from our Kitura server.
Inside the file which defines the routes,
KueryRoutes.swift, import SwiftKuery and SwiftKueryPostgreSQL:
import SwiftKuery import SwiftKueryPostgreSQL
App extension, create a connection pool of connections:
static let poolOptions = ConnectionPoolOptions(initialCapacity: 1, maxCapacity: 5) static let pool = PostgreSQLConnection.createPool(host: "localhost", port: 5432, options: [.databaseName("bookstoredb")], poolOptions: poolOptions) // Create table instance here
This creates a pool of connections for us to use to make requests to our database.
If you are on Linux, you must provide your username and password in the options for
Now you can return to the Swift-Kuery guide.