Skip to content

SQLPostgresConfiguration does not allow the postgresql:// URL scheme #250

@vzsg

Description

@vzsg

Describe the bug

When using a connection string managed by a hosted database, the provider might use either postgres:// or postgresql:// as the scheme identifier in the URL. DigitalOcean for example uses the latter with their managed databases.

This is explicitly allowed in other clients like libpq:

The URI scheme designator can be either postgresql:// or postgres://.

However, the SQLPostgresConfiguration struct only accepts the shorter version, causing an URLError to be thrown.

To Reproduce

  1. Clone a template project that has postgres set up.

    git clone [email protected]:vapor/template-fluent-postgres-leaf.git postgres-url-test
    cd postgres-url-test
    xed Package.swift
  2. Change the database configuration in configure.swift to take a postgresql:// URL instead of separate fields.

    // Typically: Environment.get("DATABASE_URL")
    let config = try SQLPostgresConfiguration(url: "postgresql://db:[email protected]:123/db?sslmode=require")
    app.databases.use(.postgres(configuration: config), as: .psql)
  3. Run the project and observe the crash.

    Error Domain=NSURLErrorDomain Code=-1000 "(null)" UserInfo={NSErrorFailingURLStringKey=postgresql://db:[email protected]:123/db?sslmode=require, NSErrorFailingURLKey=postgresql://db:[email protected]:123/db?sslmode=require}
    

Expected behavior

The URL should be parsed successfully.

Environment

  • Vapor Framework version: 4.78.1 (postgres-kit 2.12.1)
  • Vapor Toolbox version: N/A
  • OS version: macOS 13.5 (22G74)

Workaround

Manually replacing postgresql:// in the URL with postgres:// works around the issue – either by changing the connection string by hand, or replacing the URL scheme in code. The latter approach works best if the service provider rotates the credentials automatically.

.replacingOccurrences(of: "postgresql://", with: "postgres://")

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions