diff --git a/click_odoo/env_options.py b/click_odoo/env_options.py index 6b60d56..3939b0d 100644 --- a/click_odoo/env_options.py +++ b/click_odoo/env_options.py @@ -5,6 +5,7 @@ from contextlib import closing import click +import psycopg2.errors from click.decorators import _param_memo # XXX undocumented click internal from .compat import environment_manage @@ -145,15 +146,26 @@ def _configure_odoo(self, ctx): odoo.tools.config.parse_config(odoo_args) odoo.cli.server.report_configuration() + def _can_connect_to_db(self, dbname): + try: + conn = odoo.sql_db.db_connect(dbname) + with closing(conn.cursor()): + return True + except Exception: + return False + def _db_exists(self, dbname): - conn = odoo.sql_db.db_connect("postgres") - with closing(conn.cursor()) as cr: - cr._obj.execute( - "SELECT datname FROM pg_catalog.pg_database " - "WHERE lower(datname) = lower(%s)", - (dbname,), - ) - return bool(cr.fetchone()) + try: + conn = odoo.sql_db.db_connect("postgres") + with closing(conn.cursor()) as cr: + cr._obj.execute( + "SELECT datname FROM pg_catalog.pg_database " + "WHERE lower(datname) = lower(%s)", + (dbname,), + ) + return bool(cr.fetchone()) + except psycopg2.errors.InsufficientPrivilege: + return self._can_connect_to_db(dbname) def _pop_params(self, ctx): ctx.params.pop("config", None)