Heroku postgres postgis - django发布失败,原因如下:关系“spatial_ref_sys”不存在

ioekq8ef  于 2022-11-13  发布在  Go
关注(0)|答案(9)|浏览(159)

Heroku于2022年8月1日更改了其PostgreSQL扩展模式管理。(https://devcenter.heroku.com/changelog-items/2446
从那时起,我们现有的django 4.0应用程序在发布阶段部署到Heroku的每一次都失败了,但构建成功了。
有没有人遇到过同样的问题?除了重新安装postgis扩展外,有没有变通办法将新版本推到Heroku?
如果我没理解错的话,Heroku为新创建的扩展添加了一个名为“heroku_ext”的模式,因为这个扩展在我们的示例中已经存在,所以它应该不会受到影响。
当前安装的所有扩展将继续按预期工作。
通过git push跟踪发布的完整日志:

git push staging develop:master
Gesamt 0 (Delta 0), Wiederverwendet 0 (Delta 0), Pack wiederverwendet 0
remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote: -----> Using buildpacks:
remote:        1. https://github.com/heroku/heroku-geo-buildpack.git
remote:        2. heroku/python
remote: -----> Geo Packages (GDAL/GEOS/PROJ) app detected
remote: -----> Installing GDAL-2.4.0
remote: -----> Installing GEOS-3.7.2
remote: -----> Installing PROJ-5.2.0
remote: -----> Python app detected
remote: -----> Using Python version specified in runtime.txt
remote: -----> No change in requirements detected, installing from cache
remote: -----> Using cached install of python-3.9.13
remote: -----> Installing pip 22.1.2, setuptools 60.10.0 and wheel 0.37.1
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip
remote: -----> Skipping Django collectstatic since the env var DISABLE_COLLECTSTATIC is set.
remote: -----> Discovering process types
remote:        Procfile declares types -> release, web, worker
remote: 
remote: -----> Compressing...
remote:        Done: 156.1M
remote: -----> Launching...
remote:  !     Release command declared: this new release will not be available until the command succeeds.
remote:        Released v123
remote:        https://myherokuapp.herokuapp.com/ deployed to Heroku
remote: 
remote: This app is using the Heroku-20 stack, however a newer stack is available.
remote: To upgrade to Heroku-22, see:
remote: https://devcenter.heroku.com/articles/upgrading-to-the-latest-stack
remote: 
remote: Verifying deploy... done.
remote: Running release command...
remote: 
remote: Traceback (most recent call last):
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 87, in _execute
remote:     return self.cursor.execute(sql)
remote: psycopg2.errors.UndefinedTable: relation "spatial_ref_sys" does not exist
remote: 
remote: 
remote: The above exception was the direct cause of the following exception:
remote: 
remote: Traceback (most recent call last):
remote:   File "/app/manage.py", line 22, in <module>
remote:     main()
remote:   File "/app/manage.py", line 18, in main
remote:     execute_from_command_line(sys.argv)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
remote:     utility.execute()
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 440, in execute
remote:     self.fetch_command(subcommand).run_from_argv(self.argv)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 414, in run_from_argv
remote:     self.execute(*args, **cmd_options)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 460, in execute
remote:     output = self.handle(*args, **options)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 98, in wrapped
remote:     res = handle_func(*args, **kwargs)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 106, in handle
remote:     connection.prepare_database()
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 26, in prepare_database
remote:     cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis")
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/sentry_sdk/integrations/django/__init__.py", line 544, in execute
remote:     return real_execute(self, sql, params)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 67, in execute
remote:     return self._execute_with_wrappers(
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
remote:     return executor(sql, params, many, context)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
remote:     return self.cursor.execute(sql, params)
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py", line 91, in __exit__
remote:     raise dj_exc_value.with_traceback(traceback) from exc_value
remote:   File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", line 87, in _execute
remote:     return self.cursor.execute(sql)
remote: django.db.utils.ProgrammingError: relation "spatial_ref_sys" does not exist
remote: 
remote: Sentry is attempting to send 2 pending error messages
remote: Waiting up to 2 seconds
remote: Press Ctrl-C to quit
remote: Waiting for release.... failed.
To https://git.heroku.com/myherokuapp
kwvwclae

kwvwclae1#

以下是我为使用数据库备份的审查应用程序提出的解决方案,通过pg:backups:restore(如果您正在操作生产数据库,可能需要启用维护):
1.在本地复制您的审阅应用程序数据库(最近通过pg:backups:restore恢复的数据库,以便您获取所有数据):heroku pg:pull [Database URL] localdb -a [app-name]
1.将应用程序数据库配置设置为使用localdb,然后连接到psql并执行:ALTER EXTENSION "hstore" SET SCHEMA heroku_ext;。为所有现有的扩展运行此命令。
要列出所有已拉取的可用扩展,请运行\dx。您不必更改plpgsql,它是PostgreSQL的原生扩展
这是可行的,因为您在本地拥有所有权限。
1.将此版本推送回您的审阅应用程序:heroku pg:push mylocaldb [Database URL] -a [app-name] ==〉您的数据库需要为空才能执行此操作。您可以在数据为空的新评论应用上尝试此操作。这样它就可以成为您所有评论应用的新基础
1.确保一切都按预期运行(数据正确恢复),然后你可以通过pg:backups:capture生成一个新的数据库转储,并将其作为你所有新的评论应用程序的新的数据库备份。
来源:https://devcenter.heroku.com/articles/managing-heroku-postgres-using-cli
我还必须这样做,因为例如,删除hstore扩展并重新启用它对我们的情况来说不是一个可行的选择。

jk9hmnmh

jk9hmnmh2#

我已经通过覆盖postgis/ www.example.com引擎解决了这个base.py问题,我已经将以下内容放在我的应用程序中的db/base.py

from django.contrib.gis.db.backends.postgis.base import (
     DatabaseWrapper as PostGISDatabaseWrapper,
)

class DatabaseWrapper(PostGISDatabaseWrapper):
    def prepare_database(self):
        # This is the overwrite - we don't want to call the
        # super() because of a faulty extension creation
     pass

然后在我的设置中,我将DATABASES["engine"] = "app.db"
这对备份没有帮助,但至少我可以再次释放。

yh2wf1be

yh2wf1be3#

我们在尝试使用heroku pg:backups:restoreheroku pg:copy时也遇到了此问题-如果现有备份快照包含已安装的扩展,则无法恢复它们。

kfgdxczn

kfgdxczn4#

我的团队也遇到了这种情况。我们不能等Heroku来修复,所以我们做了一点粗略的手术。这并不适用于所有人,但由于我们对PostGIS的使用相当少,所以情况还不算太糟。
以下是我针对每个受影响数据库的检查清单:

  • 备份数据库
  • 打开维护模式
  • 在所有使用geography类型存储原始lat / lng数据的表上创建新的基元float
  • geography字段中的值写入新列
  • 删除postgis扩展名:DROP EXTENSION postgis CASCADE;
  • 重新创建扩展名:CREATE EXTENSION IF NOT EXISTS postgis;
  • 重新创建geography字段,并从新列填充它们
  • 删除新的临时列
  • 重新创建相应索引

目前为止一切顺利。

bq8i3lrv

bq8i3lrv5#

我们遵循了@chedli https://stackoverflow.com/a/73219273/840568提供的解决方法
但是在我们的例子中,当尝试ALTER EXTENSION postgis SET SCHEMA heroku_ext解决方法时,postgis给出了一个不允许重定位模式的错误,所以我们最终不得不执行这个额外的步骤。

UPDATE pg_extension
  SET extrelocatable = true
    WHERE extname = 'postgis';

ALTER EXTENSION "postgis" SET SCHEMA "heroku_ext";

UPDATE pg_extension
  SET extrelocatable = false
    WHERE extname = 'postgis';
cgvd09ve

cgvd09ve6#

我正在跟踪一个非常相似的问题。我一直在与heroku联系。我以前的错误提到了pgaudit扩展,现在我看到了同样的错误,你。我会在这里更新,如果我了解到一个解决方案。

System check identified no issues (5 silenced).
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 87, in _execute
return self.cursor.execute(sql)
psycopg2.errors.UndefinedTable: relation "spatial_ref_sys" does not exist
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/app/manage.py", line 40, in
main()
File "/app/manage.py", line 36, in main
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/init.py", line 446, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/init.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/base.py", line 414, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/base.py", line 460, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/base.py", line 98, in wrapped
res = handle_func(*args, **kwargs)
File "/app/.heroku/python/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 106, in handle
connection.prepare_database()
File "/app/.heroku/python/lib/python3.10/site-packages/psqlextra/backend/base.py", line 32, in prepare_database
super().prepare_database()
File "/app/.heroku/python/lib/python3.10/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 26, in prepare_database
cursor.execute("CREATE EXTENSION IF NOT EXISTS postgis")
File "/app/.heroku/python/lib/python3.10/site-packages/sentry_sdk/integrations/django/init.py", line 544, in execute
return real_execute(self, sql, params)
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/app/.heroku/python/lib/python3.10/site-packages/django_read_only/init.py", line 74, in blocker
return execute(sql, params, many, context)
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
with self.db.wrap_database_errors:
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/utils.py", line 91, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/app/.heroku/python/lib/python3.10/site-packages/django/db/backends/utils.py", line 87, in _execute
return self.cursor.execute(sql)
django.db.utils.ProgrammingError: relation "spatial_ref_sys" does not exist
Sentry is attempting to send 2 pending error messages
Waiting up to 2 seconds
Press Ctrl-C to quit
iyr7buue

iyr7buue7#

在此之前,我们还遇到了一个错误,即pgaudit stack is not empty,它要求我手动将数据库置于维护模式,以便对其进行轮换。https://devcenter.heroku.com/articles/data-maintenance-cli-commands#heroku-data-maintenances-schedule本文帮助我解决了第一个问题,但现在我与spatial_ref_sys错误处于同一条船上。

bkhjykvo

bkhjykvo8#

我有一个解决方案,不需要更改代码库,完全可以通过Heroku CLI完成
1.使用Heroku数据存储持久性工具在源数据库或heroku pg:backups:capture -a <SOURCE_APP>上创建备份。
1.确定数据库使用哪些pg扩展名(可以使用\dx从psql检查)
1.创建一个以逗号分隔的扩展名字符串(例如:(单位:fuzzystrmatch,pg_stat_statements,pg_trgm,pgcrypto,plpgsql,unaccent,uuid-ossp'
1.确保您的Heroku CLI至少更新到7.63.0版(使用heroku update进行更新)
1.运行此命令:

heroku pg:backups:restore $(heroku pg:backups public-url -a <SOURCE_APP>) DATABASE_URL --extensions '<EXTENSIONS>' -a <TARGET_APP>

1.重置TARGET_APP上的动态

mf98qq94

mf98qq949#

Heroku团队最终解决了这个问题:)

相关问题