12

This is my travis.yml file:

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

But when I deploy the repo and Travis runs, it outputs this error:

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: 

I tried adding psql -U postgres -c "ALTER USER django CREATEDB;" so that the django user would have permission to create a database, but that fails with:

ERROR:  role "django" does not exist
The command "psql -U postgres -c "ALTER USER django CREATEDB;"" failed and exited with 1 during .
Timo Tijhof
  • 10,032
  • 6
  • 34
  • 48
Richard
  • 62,943
  • 126
  • 334
  • 542
  • Did you define the user 'test' in settings file of your app ? The test database is created by the user specified by USER, so you’ll need to make sure that the given user account has sufficient privileges. – Conans May 01 '15 at 17:39
  • The settings file refers to the environment variable, which I set above. What I don't understand is that I can run `makemigrations` and `migrate` just fine (so must have adequate permissions to do that) but then I can't create the test database. – Richard May 02 '15 at 06:54
  • Got it: I had to make the user a superuser: `CREATE USER test WITH CREATEUSER PASSWORD 'test';` and then give the user permissions to create databases: `ALTER USER test CREATEDB;` – Richard May 02 '15 at 07:02
  • related: https://stackoverflow.com/q/14186055 – djvg Jun 05 '23 at 12:17

1 Answers1

19

The answer: I had to make the test user a superuser:

CREATE USER test WITH PASSWORD 'test'; 

and then give the user permissions to create databases:

ALTER USER test CREATEDB;
Barney Szabolcs
  • 11,846
  • 12
  • 66
  • 91
Richard
  • 62,943
  • 126
  • 334
  • 542