Travis:"创建测试数据库时出错:创建数据库的权限被拒绝"

Ric*_*ard 8 django travis-ci

这是我的travis.yml档案:

language: python
python:
  - "2.7"
addons:
  postgresql: "9.3"
env:
  - SECRET_KEY=test DB_NAME=dbtest DB_USER=test DB_PASS=test
before_install:
  - export DJANGO_SETTINGS_MODULE=settings.local
  - export PYTHONPATH=$HOME/builds/me/myrepo
install:
 - pip install -r requirements.txt
before_script:
 - psql -U postgres -c 'CREATE DATABASE dbtest;'
 - psql -U postgres -c "CREATE EXTENSION postgis" -d dbtest
 - psql -U postgres -c "CREATE EXTENSION postgis_topology" -d dbtest
 - psql -U postgres -c "CREATE USER test WITH PASSWORD 'test';"
 - psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE dbtest to test;"
 - cd myapp && python manage.py migrate
script:
 - python manage.py test
Run Code Online (Sandbox Code Playgroud)

但是当我部署repo并运行Travis时,它会输出以下错误:

10.96s$ pip install -r requirements.txt
0.24s$ psql -U postgres -c 'CREATE DATABASE dbtest;'
0.77s$ psql -U postgres -c "CREATE EXTENSION postgis" -d dbtest
0.09s$ psql -U postgres -c "CREATE EXTENSION postgis_topology" -d dbtest
0.01s$ psql -U postgres -c "CREATE USER test WITH PASSWORD 'test';"
0.01s$ psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE dbtest to test;"
1.89s$ cd myapp && python manage.py migrate
$ python manage.py test
Creating test database for alias 'default'...
Got an error creating the test database: permission denied to create database
Type 'yes' if you would like to try deleting the test database 'test_dbtest', or 'no' to cancel: 
Run Code Online (Sandbox Code Playgroud)

我尝试添加,psql -U postgres -c "ALTER USER django CREATEDB;"以便django用户有权创建数据库,但失败的原因是:

ERROR:  role "django" does not exist
The command "psql -U postgres -c "ALTER USER django CREATEDB;"" failed and exited with 1 during .
Run Code Online (Sandbox Code Playgroud)

Ric*_*ard 16

答案:我必须让测试用户成为超级用户:

CREATE USER test WITH PASSWORD 'test'; 
Run Code Online (Sandbox Code Playgroud)

然后授予用户创建数据库的权限:

ALTER USER test CREATEDB;
Run Code Online (Sandbox Code Playgroud)