PQ

main

A simple wrapper of libpq in Swift.
YOCKOW/SwiftPQ

What is SwiftPQ?

SwiftPQ is a simple wrapper of libpq that is a C-API to PostgreSQL. You can send query to PostgreSQL server with raw SQL, or in the Swifty way1.

Warning

UNDER DEVELOPMENT. ANY APIs MAY CHANGE IN ANY TIME.

Requirements

Usage

First of all: Establish the connection.

By UNIX Socket

import PQ

let connection = try Connection(
  unixSocketDirectoryPath: "/var/run/postgresql",
  database: databaseName,
  user: databaseUserName,
  password: databasePassword
)

Specifying domain

import PQ

let connection = try Connection(
  host: .localhost,
  database: databaseName,
  user: databaseUserName,
  password: databasePassword
)

Let's send queries!

You can see the implementations of commands in "Sources/PQ/Commands" directory. Some macros are useful to embed SQL tokens into queries. See Macros.swift.

CREATE TABLE

Raw SQL

let result = try await connection.execute(.rawSQL("""
CREATE TABLE products (
  product_no integer,
  name text,
  price numeric
);
"""))

SQL with String Interpolation

let result = try await connection.execute(.rawSQL("""
CREATE TABLE \(identifier: "my_products#1") (
  product_no integer,
  name text,
  price numeric
);
"""))

Swifty way

let result = try await connection.execute(
  .createTable(
    "myFavouriteProducts",
    definitions: [
      .column(name: "product_no", dataType: .integer),
      .column(name: "name", dataType: .text),
      .column(name: "price", dataType: .numeric),
    ],
    ifNotExists: true
  )
)

DROP TABLE

Raw SQL

let result = try await connection.execute(.rawSQL("DROP TABLE my_table;"))

SQL with String Interpolation

let result = try await connection.execute(.rawSQL("DROP TABLE \(identifier: "my_table#1");"))

Swifty way

let result = try await connection.execute(.dropTable("my_old_table", ifExists: true))

License

MIT License.
See "LICENSE.txt" for more information.

Footnotes

  1. See also SQLGrammarCorrespondingTypes.md

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Thu Apr 02 2026 04:41:03 GMT-0900 (Hawaii-Aleutian Daylight Time)