简体   繁体   English

Heroku上的Django无法使用不同的架构连接到Postgres

[英]Django on Heroku can't connect to Postgres with different schema

I devoted today to migrating my app from my local environment to Heroku. 我今天致力于将我的应用程序从本地环境迁移到Heroku。 It's been frustrating and fun at the same time, but now I'm very stuck. 同时令人沮丧和有趣,但是现在我很困。

I have a schema called DCPViews which I want the DB connections to default to when running queries. 我有一个名为DCPViews的架构,我希望在运行查询时将数据库连接默认设置DCPViews架构。 I've read all the relevant tutorials / posts / tips and here's where I am: 我已经阅读了所有相关的教程/帖子/技巧,这就是我的位置:

DB hierarchy 数据库层次结构

postgres (default system database) 
  -> DCP (app database) 
    -> DCP (base tables schema) 
    -> DCPViews (views layer schema) 

settings.py settings.py

import django_heroku
import dj_database_url

...

DATABASES = {}

# DATABASE_URL = 'postgres://<user>:<pass>@<host>:<port>/<db_name>?currentSchema=<schema>'
DATABASE_URL = 'postgres://' + \
                config('DB_USER') + ':' + \
                config('DB_PASSWORD') + '@' + \
                config('DB_HOST') + ':' + \
                config('DB_PORT') + '/' + \
                config('DB_NAME') + \
                '?currentSchema=' + config('DB_SCHEMA_NAME')

DATABASES['default'] = dj_database_url.config(default=DATABASE_URL, ssl_require=True)

...

# Configure Django App for Heroku
django_heroku.settings(locals())

The problem 问题

I've tried everything to get Heroku to use the correct DATABASE_URL (with currentSchema = DCPViews ), but no luck. 我已经尽一切努力使Heroku使用正确的DATABASE_URL(带有currentSchema = DCPViews ),但是没有运气。 I don't have DB permissions to create new roles nor set a search path for Heroku's default DB user. 我没有数据库权限来创建新角色,也没有为Heroku的默认数据库用户设置搜索路径。 It also won't let me export DATABASE_URL manually and doesn't seem to accept the value I pass in the settings.py file. 它还不允许我手动导出DATABASE_URL ,并且似乎不接受我在settings.py文件中传递的值。 The heroku config -s command always returns the same DATABASE_URL value. heroku config -s命令始终返回相同的DATABASE_URL值。

Everything works fine in my local environment, but this is a major snag. 在我的本地环境中一切正常,但这是一个主要障碍。 How can I get Heroku to use the correct search_path or default to the DCPViews schema when running queries? 运行查询时,如何让Heroku使用正确的search_path或默认使用DCPViews模式?

Update 更新

I'm send send the DB credentials and currentSchema I want to use in my settings.py file, but Heroku seems to ignore them and overwrites them when it creates the DATABASES['default'] key. 我正在发送要在我的settings.py文件中使用的数据库凭证和currentSchema ,但是Heroku在创建DATABASES['default']键时似乎忽略了它们并覆盖了它们。 Here's the Django debug when I load a page in my browser: 这是在浏览器中加载页面时的Django调试:

DATABASES   
{'default': {'ATOMIC_REQUESTS': False,
             'AUTOCOMMIT': True,
             'CONN_MAX_AGE': 600,
             'ENGINE': 'django.db.backends.postgresql_psycopg2',
             'HOST': '<host_name>',
             'NAME': '<db_name>',
             'OPTIONS': {'sslmode': 'require'},
             'PASSWORD': '********************',
             'PORT': 5432,
             'TEST': {'CHARSET': None,
                      'COLLATION': None,
                      'MIRROR': None,
                      'NAME': None},
             'TIME_ZONE': None,
             'USER': '<user_name>'}}
DATABASE_URL    
'postgres://<user>:<pass>@<host>:<port>/<db_name>?currentSchema=<schema>'

I cannot comment yet, but since your settings file is using an environment file, you can just go to your heroku app in heroku.com and go to your database that is connected to your app. 我无法发表评论,但是由于您的设置文件使用的是环境文件,因此您只需转到heroku.com上的heroku应用程序,然后转到与应用程序连接的数据库即可。 Then go to settings and click reveal config vars . 然后转到设置,然后单击reveal config vars Change your DATABASE_URL to whatever your need it to be (mind you it wil probably be just an extension off of your actual heroku postgres database url) 将您的DATABASE_URL更改为您需要的任何值(请注意,它可能只是您实际的heroku postgres数据库URL的扩展)

Well, I was on the right path. 好吧,我走在正确的道路上。 I added the search_path to the ROLE and that did the trick: 我将search_path添加到ROLE中,并且达到了目的:

ALTER ROLE <role_name> SET search_path = <go, pirates>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM