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

5

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

我需要类似于postgis://...的DATABASE_URL来供我的Django环境使用。

我最新的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流水线出现错误响应:

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

你为什么认为需要以 postgis://... 开头的 URI?这只是 pg 的扩展,而不是单独的协议。 - Jakub Kania
@JakubKania 因为我使用 dj-database-url 库来加载 DATABASE_URL。 - Dima Chuvardinsky
2个回答

2

尝试从Django文档中使用替代选项:

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

class Migration(migrations.Migration):

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

谢谢,我将其添加到我的迁移中,但是在 gitlab-runner 中出现了新错误:django.core.exceptions.ImproperlyConfigured: 找不到 GDAL 库(尝试过 "gdal"、"GDAL"、"gdal2.2.0"、"gdal2.1.0"、"gdal2.0.0"、"gdal1.11.0"、"gdal1.10.0"、"gdal1.9.0")。是否已安装 GDAL?如果已安装,请尝试在设置中设置 GDAL_LIBRARY_PATH。 - Dima Chuvardinsky
尝试安装 apt-get -y install binutils libproj-dev gdal-bin - jimla

2
您可以使用已经安装了PostGIS的服务,并像这样安装二进制文件:

您可以使用已经安装了PostGIS的服务,并安装二进制文件,如下所示:

最初的回答
services:
  - mdillon/postgis:9.4

variables:
  POSTGRES_DB: "db_name"
  POSTGRES_USER: "db_user"
  POSTGRES_PASSWORD: "db_pwd"
  POSTGRES_HOST: "db"
  POSTGRES_PORT: "5432"
  DATABASE_URL: 
  "postgres://db_user:db_pwd@mdillon__postgis/db_name"

before_script:
  - apt-get update -qy
  - apt-get -y install binutils libproj-dev gdal-bin
  - python -V
  - pip3 install -r requirements.txt

References: -https://www.hackzine.org/postgis-on-gitlab-ci.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接