个人偏向配置dbfilter,
db_name
指定多个数据库,可以使用逗号隔开,如db_name=db1,db2
,但是db1,和db2,必须实际存在数据库中,不然启动报错,这就是我不喜欢使用的原因dbfilter
使用正则表达式的方式可以指定多个数据库:dbfilter=db1|db2
补充:
dbfilter 有时候无法获取所有的数据库名:后来查到的原因是使用不同的数据库账号创建的数据库不能交叉使用,即使是superuser
;
获取所有数据库的代码,注意sql部分中有对datdba
做筛选
def list_dbs(force=False):
if not odoo.tools.config['list_db'] and not force:
raise odoo.exceptions.AccessDenied()
if not odoo.tools.config['dbfilter'] and odoo.tools.config['db_name']:
# In case --db-filter is not provided and --database is passed, Odoo will not
# fetch the list of databases available on the postgres server and instead will
# use the value of --database as comma seperated list of exposed databases.
res = sorted(db.strip() for db in odoo.tools.config['db_name'].split(','))
return res
chosen_template = odoo.tools.config['db_template']
templates_list = tuple(set(['postgres', chosen_template]))
db = odoo.sql_db.db_connect('postgres')
with closing(db.cursor()) as cr:
try:
cr.execute("select datname from pg_database where datdba=(select usesysid from pg_user where usename=current_user) and not datistemplate and datallowconn and datname not in %s order by datname", (templates_list,))
res = [odoo.tools.ustr(name) for (name,) in cr.fetchall()]
except Exception:
_logger.exception('Listing databases failed:')
res = []
return res