python - 如何在 gitlab-ci.yml 中将 postgis 扩展添加到 postgresql 数据库

标签 python django postgresql postgis gitlab-ci

我在 gitlab-ci.yml 文件中设置测试阶段,但在为 postgresql 数据库配置 postgis 扩展时出现错误。

我的 Django 环境需要 DATABASE_URL,例如 postgis://...

我最新版本的gitlab-ci.yml:

image: python:3.6

services:
  - postgres:9.6

variables:
  POSTGRES_DB: test_db
  POSTGRES_USER: test_user
  POSTGRES_PASSWORD: test_pass
  DATABASE_URL: "postgis://test_user:test_pass@postgres:5432/test_db"

stages:
  - test
  - build

test:
  stage: test
  script:
    - apt-get update -qy
    - apt-get -y install binutils libproj-dev gdal-bin postgis*
    - pip3 install pipenv
    - pipenv install --dev
    - export DATABASE_URL=$DATABASE_URL
    - pipenv run test

Gitlab Pipeline错误响应:

Running with gitlab-runner 11.5.0 (3afdaba6)
  on docker-auto-scale fa6cab46
Using Docker executor with image python:3.6 ...
Starting service postgres:9.6 ...
Pulling docker image postgres:9.6 ...
$ export DATABASE_URL=$DATABASE_URL
$ pipenv run test
/root/.local/share/virtualenvs/pixel-api-yo4gnz48/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use "pip install psycopg2-binary" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.
  """)
Creating test database for alias 'default'...
Traceback (most recent call last):
  File "/root/.local/share/virtualenvs/pixel-api-yo4gnz48/lib/python3.6/site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
psycopg2.OperationalError: could not open extension control file "/usr/share/postgresql/9.6/extension/postgis.control": No such file or directory

django.db.utils.OperationalError: could not open extension control file "/usr/share/postgresql/9.6/extension/postgis.control": No such file or directory

最佳答案

尝试使用 django documentation 中的替代选项:

from django.contrib.postgres.operations import CreateExtension
from django.db import migrations

class Migration(migrations.Migration):

    operations = [
        CreateExtension('postgis'),
        ...
    ]

关于python - 如何在 gitlab-ci.yml 中将 postgis 扩展添加到 postgresql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54087590/

相关文章:

sql - PostgreSQL 返回与查询日期准确或最接近的日期

python - 在哪里调用对模型具有依赖性的外部 API?

python - Python 垃圾何时收集?

python - 如何在执行过程中不提供输入的情况下运行 aws-nuke?

python - 导入错误 : No module named 'config.settings' ; 'config' is not a package

django - 使用 Wagtail CMS 片段但隐藏在管理面板中

java - 数据库连接: protocoling data for hundereds to million objects

mysql - SQL 中 bool 型与整数的最佳实践

sql - 表之间的最近值

SQL请求按列中的值查找表中的项目