'when running tests in django and PostgreSQL DB can't create database
I'm having a problem with running unit tests in django while using ElephantSQL,
when running command python manage.py runserver
everything works just fine,
I'm able to connect to the server without any problem
but when running the command python manage.py test
I'm getting this error below:
C:\Users\Sman9\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\siteid running initialization queries against the production database when it's not needed (for example, when running tests). Djang
warnings.warn(
Got an error creating the test database: permission denied to create database
Creating test database for alias 'default'...
C:\Users\Sman9\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\django\db\backends\postgresql\base.py:323: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running
tests). Django was unable to create a connection to the 'postgres' database and will use the first PostgreSQL database instead.
warnings.warn(
Got an error creating the test database: permission denied to create database```
my databases setting in settings.py file
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '####',
'USER': '#####',
'PASSWORD': '#############',
'HOST': 'chunee.db.elephantsql.com',
'PORT':'',
}
}
}
Solution 1:[1]
Make sure that you have granted all required privileges to a user you have created for managing your database.
Using psql you can do so with the following command:
GRANT ALL PRIVILEGES ON DATABASE mydb TO myuser;
Additionally, you ought to have psycopg2 installed for the postgres DB to pair with Django.
In a terminal execute the following:
pip install psycopg2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | richardec |