fallback to pg8000 if native postgres module is not installed

Issue #3126 wontfix
Sorin Sbarnea
created an issue

Instead of hardcoding the backend engine to use for postgresql, sql alchemy should try to use pg8000 if the native module is not present.

Comments (7)

  1. Michael Bayer repo owner

    ideally SQLAlchemy wouldn't even have "postgresql://" without a driver available, that style is mostly legacy. an app should take care to ensure that the expected drivers are installed. If you really want an "any" type of thing, you can write a checker on the app side.

  2. Sorin Sbarnea reporter

    I am wondering how one would be able to use SQLAlchemy without a database URI. It would be nice if you could include a link to a doc page that documents the "ideal" way of connecting to the database (the one without URI). Thanks.

  3. Michael Bayer repo owner

    I don't understand.

    Do you mean just the "/database" portion of the URL ?

    what driver(s) ? for the PG ones it can just be omitted, if the driver understands the "database" parameter being omitted.

  4. Sorin Sbarnea reporter

    Use case: as a user of postgresql sqlalchemy user I do want to be able to connect to a database without having to hardcode the driver or to add driver detection code in my script.

    Now sqlalchemy supports supports two major postgresql drivers: psycopg2 and pg8000. The psycopg2 is the default driver, which is used if you do not specify the driver using the "+drivername" in the URI's schema.

    So, this is a request to add a very simple improvement to the postgresql:// schema: to try to use the pg8000 driver if the psycopg2 module is not installed.

    This is not about changing the default driver, just to prevent a connection failure when there is a way to connect to the database.

    Yes, you are right that this can be implemented inside the calling script / application, but this would make the implementation more verbose, duplicating the same piece of code everywhere.

    Please understand that not all SQLAlchemy users do require a specific driver, and picking one of the available would follow KISS principle, something python is quite good about.

  5. Michael Bayer repo owner

    So, this is a request to add a very simple improvement to the postgresql:// schema: to try to use the pg8000 driver if the psycopg2 module is not installed.

    this is guessing on the part of the library, and a surprise for a user that expects psycopg2 to be installed. there are many differences in features and behavior between these two drivers. as I said before, I disagree that "postgresql://" should even be allowed period in the ideal case, this is legacy. the user should specify the backend + dbapi at all times.

    Please understand that not all SQLAlchemy users do require a specific driver, and picking one of the available would follow KISS principle, something python is quite good about.

    it also stresses "explicit over implicit" ...I'd also argue that having the user simply type the name of the driver they'd like is "simpler" than SQLA having additional logic to guess which one to use, maintaining a "favorites list" of drivers for other backends like MySQL, which order do we guess among the seven or so MySQL drivers we have? what if a user specifies an mssql URL that has an ODBC DSN, but our "simple" fallback behavior then falls back to pymssql, but then the URL is invalid because pymssql doesnt use DSN's....what if the psycopg2 URL specifies "encoding" , but then pg8000 doesn't accept that argument, and in fact silently ignores it....there is lots that I'd ask you to consider here.

  6. Log in to comment