Skip to content

Accept connecting with PostgreSQL URLs instead of connection strings #2090

Description

@luizs81

Would be possible to accept the URI as a valid connection string?

postgresql://[user[:password]@][netloc][:port][,...][/dbname][?param1=value1&...]

This is how Heroku provides access to its databases, and due the fact the values can (will?) change without announcement, it becomes quite cumbersome to manage it to the current "key value" format.

If you guys think it's a good idea, I would be happy to submit a PR to enhance the NpgsqlConnectionStringBuilder class.

Thanks!

Activity

  1. austindrenski commented on Aug 2, 2018

    @austindrenski
    Contributor

    I really like this idea. Maintaining different connection strings for tools like psql/Rider/DataGrip and Npgsql/EF Core can be a real pain.

    There are probably some ADO.NET requirements to consider, but there are examples where Npgsql-specific APIs are implemented because the standard was insufficient.

    Related

    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/connection-strings
    https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/connection-string-syntax

  2. YohDeadfall commented on Aug 2, 2018

    @YohDeadfall
    Contributor

    Makes sense, but I would like to discuss the design first here. I think there should be a constructor which accepts Uri as a parameter.

  3. austindrenski commented on Aug 2, 2018

    @austindrenski
    Contributor

    @YohDeadfall Then parse Uri into the current connection string format?

    Parsing with Span<T> could help limit the overhead of the Uri constructor vs the standard constructor.

  4. roji commented on Aug 2, 2018

    @roji
    Member

    I don't see any reason not to support this...

    However, we should avoid having a constructor that accepts URIs, for perf reasons... The pool manager keys by string, which would mean that every (pooled) connection open has to do Uri.ToString() - not ideal. At some point there was a constructor that accepted an instance of NpgsqlConnectionStringBuilder and it encouraged bad perf behavior so it was removed.

    I think this issue is more a matter of detecting URIs and then internally constructing instances of NpgsqlConnectionStringBuilder from them, it shouldn't be too hard...

  5. roji commented on Aug 2, 2018

    @roji
    Member

    Another note on perf...

    At the moment you can only set connection strings on a connection with a simple string. The first time a string is seen, it's parsed into an NpgsqlConnectionStringBuilder (this is an expensive one-time operation), which is then cached in the pool. The next time the string is found by the pool manager, and the cached parsed NpgsqlConnectionStringBuilder is reused. I think we should stick to this model which is very efficient, and only change the one-time parsing process to support URIs.

  6. YohDeadfall commented on Aug 2, 2018

    @YohDeadfall
    Contributor

    Uri.ToString returns the original string when it doesn't contain Unicode characters.

  7. austindrenski commented on Aug 2, 2018

    @austindrenski
    Contributor

    I think this issue is more a matter of detecting URIs and then internally constructing instances of NpgsqlConnectionStringBuilder from them, it shouldn't be too hard...

    Do we need to support anything other than postgresql://?

  8. roji commented on Aug 2, 2018

    @roji
    Member

    Uri.ToString returns the original string when it doesn't contain Unicode characters.

    Hmm, OK, so maybe it's not a problem. I still don't see great value in a constructor accepting a Uri (the user can just do ToString() themselves) but as long as it's OK for perf I'm not really against it either...

  9. roji commented on Aug 2, 2018

    @roji
    Member

    Do we need to support anything other than postgresql://?

    I don't think so - we should aim to support the URIs as described in the PostgreSQL docs.

    Note that for now we don't support multiple hosts or ports, although there's a PR pending for that.

  10. luizs81 commented on Aug 3, 2018

    @luizs81
    Author

    @YohDeadfall

    I believe we could use Uri.TryCreate to detect whenever the argument is a Uri or not, and then proceed accordingly.

  11. YohDeadfall commented on Aug 3, 2018

    @YohDeadfall
    Contributor

    @roji @luizs81 Does it mean that the existing string builder constructor will take a string and check that it's possible to construct an Uri from it?

  12. roji commented on Aug 4, 2018

    @roji
    Member

    I'm not sure about the specific implementation details, but ideally the URI handling logic would be handled outside NpgsqlStringConnectionBuilder, to keep that class concerned with what DbStringConnectionBuilders usually do. However, if a URI is detected, an instance of NpgsqlConnectionStringBuilder would be constructed and used internally to hold all the parsed configuration options, just as it is done today.

    Note that accessing NpgsqlConnection.ConnectionString should still provide the connection string as a URI, and ideally we'd also elide the password after connection open just like we do today.

  13. added this to the Backlog milestone on Aug 4, 2018
  14. YohDeadfall commented on Aug 4, 2018

    @YohDeadfall
    Contributor

    I like the idea.

    @luizs81 Would you like to implement it?

  15. luizs81 commented on Aug 7, 2018

    @luizs81
    Author

    @YohDeadfall

    I have to say I have no experience yet contributing to open source projects. I'm excited about finally having a chance to help, but probably I won't get it right in a first trial.

    So, if you guys don't mind a bit of back and forth during the PR review, I'm glad in doing it. Otherwise, I don't mind leaving this to someone with more experience.

  16. YohDeadfall commented on Aug 7, 2018

    @YohDeadfall
    Contributor

    @luizs81 It's fine to change the code based on the feedback. So feel free to open a pull request, and we will help you (:

  17. luizs81 commented on Aug 7, 2018

    @luizs81
    Author

    Cool!
    I will be working on that soon and hopefully open the PR in the following days.

  18. bezchristo commented on Jun 14, 2019

    @bezchristo

    @luizs81, @YohDeadfall Was there ever a PR created for this?

  19. YohDeadfall commented on Jun 14, 2019

    @YohDeadfall
    Contributor

    No one has opened a PR, so it's still up for grabs.

  20. jgilchrist commented on Jul 7, 2019

    @jgilchrist

    @YohDeadfall I've filed a early WIP (#2512) - it would be great to get your thoughts on the approach!

  21. pedrowindisch commented on Mar 25, 2024

    @pedrowindisch
    Contributor

    It seems like this issue is still up for grabs. I'd like to start working on it if possible. =)

    Since this issue has been inactive for some time, I'd like to know if there is something to be aware of before starting working on it.

    Or can I just go ahead and start implementing the connection URI support?

    Thanks!

  22. roji commented on Mar 26, 2024

    @roji
    Member

    @pedrowindisch please take a look at the earlier two PRs. This isn't a trivial thing to implement, and requires quite a bit of effort and design thinking; we spent time reviewing the previous two PRs, but unfortunately they were abandoned by their original authors. So while we welcome contributions, please only start working on this if you're determined to carry it through (also keep in mind that we may not have time to review right away, so this make take a while).

    Otherwise, if you're looking for something to contribute, there are probably smaller work items that may be a better fit.

  23. changed the title [-]Accept URI format as valid connection string[/-] [+]Accept connecting with PostgreSQL URLs instead of connection strings[/+] on Nov 17, 2024
  24. added theissue type on Jun 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions