Skip to content

[PYD-141] PostgresDsn doesn't accept valid socket for host host #3900

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

Closed
mangecoeur opened this issue Mar 15, 2022 · 8 comments
Closed

[PYD-141] PostgresDsn doesn't accept valid socket for host host #3900

mangecoeur opened this issue Mar 15, 2022 · 8 comments
Labels
bug V1 Bug related to Pydantic V1.X bug V2 Bug related to Pydantic V2 non-breaking-change Addressing this issue does not require a breaking change

Comments

@mangecoeur
Copy link

mangecoeur commented Mar 15, 2022

Checks

  • [x ] I added a descriptive title to this issue
  • [ x] I have searched (google, github) for similar issues and couldn't find anything
  • [ x] I have read and followed the docs and still think this is a bug

Bug

It is valid to give a unix socket file as a host when connecting to Postgres (e.g. via sqlalchemy) but PostgresDsn throws a 'URL host invalid' error.

from pydantic import BaseSettings, PostgresDsn

class PGSettings(BaseSettings):
    dsn: PostgresDsn = 'postgresql://derp@/databasename?host=/var/run/postgresql'

Output of python -c "import pydantic.utils; print(pydantic.utils.version_info())":

pydantic version: 1.9.0
            pydantic compiled: True
                 install path: /opt/conda/envs/geneap-1/lib/python3.9/site-packages/pydantic
               python version: 3.9.9 | packaged by conda-forge | (main, Dec 20 2021, 02:41:03)  [GCC 9.4.0]
                     platform: Linux-5.11.0-36-generic-x86_64-with-glibc2.31
     optional deps. installed: ['dotenv', 'typing-extensions']

PYD-141

@mangecoeur mangecoeur added the bug V1 Bug related to Pydantic V1.X label Mar 15, 2022
@mangecoeur
Copy link
Author

P.S. Valid format of postgres connection string:

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

References

https://stackoverflow.com/questions/27037990/connecting-to-postgres-via-database-url-and-unix-socket-in-rails

https://www.postgresql.org/docs/current/libpq-connect.html

Browsing the code I see that PostgresDsn subclasses AnyUrl, I think it's quite possible to construct a valid Postgres connection string that is not a valid URL.

@Hajbo
Copy link

Hajbo commented Mar 28, 2022

I've encountered the same thing a few days ago.
It seems like this issue has been around for a while, see #960

@wasauce
Copy link

wasauce commented May 24, 2022

Ran into this due to google cloud. Seems like #960 was closed

Do you think the community would be open to a PR to support a new PostgresDsn -- e.g. GoogleCloudPostgresDsn

@danizen
Copy link

danizen commented Dec 22, 2022

Yup - just ran into this on https://github.com/danizen/fastapi-bakeoff

@samuelcolvin
Copy link
Member

My guess would be that this is fixed in v2 she therefore main, PR welcome to add a unit test to check.

@pgiraud
Copy link
Contributor

pgiraud commented Feb 3, 2023

My guess would be that this is fixed in v2 she therefore main, PR welcome to add a unit test to check.

I would have been happy to add a unit test but unfortunately such a DNS still isn't considered valid.

>>> PGSettings(dsn="postgresql://derp@/databasename?host=/var/run/postgresql")
dsn
  Input should be a valid URL, empty host [type=url_parsing, input_value='postgresql://derp@/datab...ost=/var/run/postgresql', input_type=str]

However, what's new is that the following is considered valid in v2 (which is not the case in v1):

PGSettings(dsn="postgresql://derp@%2Fvar%2Frun%2Fpostgresql/databasename")

@samuelcolvin
Copy link
Member

Thanks for investigating, looks like this needs to be fixed in pydantic-core, I've created pydantic/pydantic-core#378.

PR welcome to fix that.

@Kludex Kludex added this to the Version 2 Issues milestone May 1, 2023
@Kludex Kludex added the bug V2 Bug related to Pydantic V2 label May 1, 2023
@dmontagu dmontagu added the non-breaking-change Addressing this issue does not require a breaking change label May 18, 2023
@dmontagu dmontagu changed the title PostgresDsn doesn't accept valid socket for host host [PYD-141] PostgresDsn doesn't accept valid socket for host host Jun 28, 2023
@dmontagu dmontagu added Bug and removed v2-reviewed labels Jun 28, 2023
leothomas pushed a commit to SkyTruth/cerulean-cloud that referenced this issue Nov 21, 2023
@sydney-runkle
Copy link
Contributor

We’re no longer actively developing Pydantic V1 (although it will continue to receive security fixes for the next year or so), so we’re closing issues solely related to V1. If you really think this shouldn’t be closed, please comment or create a new issue 🚀.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug V1 Bug related to Pydantic V1.X bug V2 Bug related to Pydantic V2 non-breaking-change Addressing this issue does not require a breaking change
Projects
None yet
Development

No branches or pull requests

9 participants