Skip to content

Commit 80ab773

Browse files
authored
Enable libpq-compatible "postgresql" URL schemes (#251)
Enable use of libpq-compatible postgresql URL schemes. Fixes #250
1 parent 81d1106 commit 80ab773

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

Sources/PostgresKit/SQLPostgresConfiguration.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ public struct SQLPostgresConfiguration {
6464
/// The aliases always have the same semantics as the "canonical" modes, despite any differences
6565
/// suggested by their names.
6666
///
67+
/// Also for compatibility, the URL scheme may also be `postgresql` or `postgresql+uds`.
68+
///
6769
/// > Note: It is possible to emulate `libpq`'s definitions for `prefer` (TLS if available with
6870
/// > no certificate verification), `require` (TLS enforced, but also without certificate
6971
/// > verification) and `verify-ca` (TLS enforced with no hostname verification) by manually
@@ -94,7 +96,7 @@ public struct SQLPostgresConfiguration {
9496
}
9597

9698
switch comp.scheme {
97-
case "postgres", "postgres+tcp":
99+
case "postgres", "postgres+tcp", "postgresql", "postgresql+tcp":
98100
guard let hostname = comp.host, !hostname.isEmpty else {
99101
throw URLError(.badURL, userInfo: [NSURLErrorFailingURLErrorKey: url, NSURLErrorFailingURLStringErrorKey: url.absoluteString])
100102
}
@@ -104,7 +106,7 @@ public struct SQLPostgresConfiguration {
104106
database: url.lastPathComponent.isEmpty ? nil : url.lastPathComponent,
105107
tls: try decideTLSConfig(from: comp.queryItems ?? [], defaultMode: "prefer")
106108
)
107-
case "postgres+uds":
109+
case "postgres+uds", "postgresql+uds":
108110
guard (comp.host?.isEmpty ?? true || comp.host == "localhost"), comp.port == nil, !comp.path.isEmpty, comp.path != "/" else {
109111
throw URLError(.badURL, userInfo: [NSURLErrorFailingURLErrorKey: url, NSURLErrorFailingURLStringErrorKey: url.absoluteString])
110112
}

Tests/PostgresKitTests/SQLPostgresConfigurationTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ final class SQLPostgresConfigurationTests: XCTestCase {
6161
XCTAssertTrue(config.coreConfiguration.tls.isEnforced)
6262
}
6363

64+
XCTAssertNoThrow(try SQLPostgresConfiguration(url: "postgresql://test_username@test_hostname"))
65+
XCTAssertNoThrow(try SQLPostgresConfiguration(url: "postgresql+tcp://test_username@test_hostname"))
66+
XCTAssertNoThrow(try SQLPostgresConfiguration(url: "postgresql+uds://test_username@/tmp/postgres.sock"))
67+
6468
XCTAssertThrowsError(try SQLPostgresConfiguration(url: "postgres+tcp://test_hostname"), "should fail when username missing")
6569
XCTAssertThrowsError(try SQLPostgresConfiguration(url: "postgres+tcp://test_username@test_hostname?tlsmode=absurd"), "should fail when TLS mode invalid")
6670
XCTAssertThrowsError(try SQLPostgresConfiguration(url: "postgres+uds://localhost/tmp/postgres.sock?tlsmode=require"), "should fail when username missing")

0 commit comments

Comments
 (0)