Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
vzsg opened this issue Aug 14, 2023 · 0 comments · Fixed by #251
Closed

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

vzsg opened this issue Aug 14, 2023 · 0 comments · Fixed by #251
Labels
bug Something isn't working

Comments

@vzsg
Copy link
Member

vzsg commented Aug 14, 2023

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 git@github.com: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:xyz@digitalocean.com: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:xyz@digitalocean.com:123/db?sslmode=require, NSErrorFailingURLKey=postgresql://db:xyz@digitalocean.com: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://")
@vzsg vzsg added the bug Something isn't working label Aug 14, 2023
gwynne added a commit that referenced this issue Aug 14, 2023
Enable use of libpq-compatible postgresql URL schemes. Fixes #250
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant