503
When Django runs the test suite, it creates a new database, in your case test_finance
. The postgres user with username django
does not have permission to create a database, hence the error message.
When you run migrate
or syncdb
, Django does not try to create the finance
database, so you donβt get any errors.
You can add the createdb permission to the django user by running the following command in the postgres shell as a superuser (hat tip to this stack overflow answer).
=> ALTER USER django CREATEDB;
Note: The username used in the ALTER USER <username> CREATEDB;
command needs to match the database user in your Django settings files. In this case, the original poster, had the user as django
the above answer.
26
I have found interesting solution to your problem.
In fact for MySQL you can grant privileges for non-existing database.
So you can add name βtest_financeβ for your test database in your settings:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'finance', # Or path to database file if using sqlite3.
'USER': 'django', # Not used with sqlite3.
'PASSWORD': 'mydb123', # Not used with sqlite3.
'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '', # Set to empty string for default. Not used with sqlite3.
'TEST': {
'NAME': 'test_finance',
},
}
}
start MySQL shell as the root user:
mysql -u root -p
and now grant all privileges to this non-existing database in MySQL:
GRANT ALL PRIVILEGES ON test_finance.* TO 'django'@'localhost';
Now Django will start tests without any problems.
- [Django]-Django CMS fails to synch db or migrate
- [Django]-Get user profile in django
- [Django]-Can I access constants in settings.py from templates in Django?
10
If the database is MySQL then these two changes will get things done.
1.Open mysite/mysite/settings.py
Your database settings should have an additional 'TEST'
block as shown with projectname_test
.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myproject',
'USER': 'chandan',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': '3306',
'TEST': {
'NAME': 'myproject_test',
},
}
}
2.Type the below command using mysql command prompt or mysql workbench to give all privilages to the user specified in settings.py
GRANT ALL PRIVILEGES ON myproject_test.* TO 'chandan'@'localhost';
Now you can run the below command:
python manage.py test polls
- [Django]-How to save pillow image object to Django ImageField?
- [Django]-ValueError: The field admin.LogEntry.user was declared with a lazy reference
- [Django]-Python (and Django) best import practices
10
In the case of Postgres, the user must have createdb
permission.
ALTER ROLE miriam CREATEDB;
See this documentation: https://docs.djangoproject.com/en/2.0/topics/testing/overview/#the-test-database
- [Django]-Annotate a queryset with the average date difference? (django)
- [Django]-Django β what is the difference between render(), render_to_response() and direct_to_template()?
- [Django]-Getting Values of QuerySet in Django
7
In my case, I donβt know why GRANT PRIVILEGES
solutions donβt work with Python 3.7.2, Django 2.1.7, and MySQL 5.6.23.
So I decided to use SQLite as a TEST
database:
DATABASES = {
'default': {
'NAME': 'productiondb',
'ENGINE': 'mysql.connector.django', # 'django.db.backends.mysql'
'USER': '<user>',
'PASSWORD': '<pass>',
'HOST': 'localhost',
'PORT': 3306,
'OPTIONS': {
'autocommit': True,
},
'TEST': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
},
}
}
After that, the tests can run without trouble:
$ python manage.py test
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
Destroying test database for alias 'default'...
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Process finished with exit code 0
- [Django]-Celery. Decrease number of processes
- [Django]-Why am I getting this error in Django?
- [Django]-How to use subquery in django?
5
Wow so combining all of the answers here with a little tweaking finally got me to a working solution for docker-compose, django, and postgresβ¦
First the postgres command given by noufal valapra is not correct (or maybe just not current), it should be:
ALTER USER docker WITH CREATEDB;
In the case of a docker-compose setup, this will go in the init.sql file, this is what mine looks like:
CREATE USER docker;
ALTER USER docker WITH CREATEDB;
CREATE DATABASE djangodb;
GRANT ALL PRIVILEGES ON DATABASE djangodb TO docker;
Then the Dockerfile for postgres looks like this:
FROM postgres:10.1-alpine
COPY init.sql /docker-entrypoint-initdb.d/
Then the Django settings.py has this entry:
if 'RDS_DB_NAME' in os.environ:
INTERNAL_DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
and the docker-compose looks like this:
version: β3.6β
services:
postgresdb:
build:
context: ./
dockerfile: ./Dockerfile-postgresdb
volumes:
- postgresdata:/var/lib/postgresql/data/
django:
build:
context: ../
dockerfile: ./docker/Dockerfile
environment:
- RDS_DB_NAME=djangodb
- RDS_USERNAME=docker
- RDS_PASSWORD=docker
- RDS_HOSTNAME=postgresdb
- RDS_PORT=5432
stdin_open: true
tty: true
depends_on:
- postgresdb
volumes:
postgresdata:
- [Django]-Where does pip install its packages?
- [Django]-Migrating Django fixtures?
- [Django]-Sending post data from angularjs to django as JSON and not as raw content
5
Switch over to the postgres account on your server by typing:
sudo -i -u postgres
You can now access a Postgres prompt immediately by typing:
psql
Now type
ALTER USER username CREATEDB;
- [Django]-How to set a value of a variable inside a template code?
- [Django]-Querying django migrations table
- [Django]-Python Asyncio in Django View
4
If you are using docker-compose
what worked for me was the following:
ALTER ROLE username CREATEDB;
GRANT ALL PRIVILEGES ON test_database_name.* TO 'username';
or
ALTER ROLE username CREATEDB;
GRANT ALL PRIVILEGES ON *.* TO 'username'@'%';
My settings looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'database_name',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'db',
'PORT': '3306',
}
}
and my docker-compose.yml
looks as follows:
version: '3'
services:
web:
build: .
command: './wait_for_db_and_start_server.sh'
env_file: env_web
working_dir: /project_name
links:
- db
volumes:
- .:/volume_name
ports:
- "8000:8000"
depends_on:
- db
db:
image: mysql:5.7
restart: always
env_file: env_db
working_dir: /db
volumes:
- ./Dump.sql:/db/Dump.sql
ports:
- "3306:3306"
- [Django]-'pip' is not recognized as an internal or external command
- [Django]-How to save pillow image object to Django ImageField?
- [Django]-Many-To-Many Fields View on Django Admin
2
I ran into the same problem a while ago.
This is how I was able to solve it on django 3.10.6
First get into your dbshell via terminal by using this command
python3 manage.py dbshell
This will take you to your projects db ie if you have created one.
The other way to get to your db is:
sudo su postgres
then:
psql
This will take you to the default database. Then connect to your projects db by:
\c projectdb
At this point use this command to fix your error:
ALTER USER you CREATEDB;
Quit your db with β\qβ; exit postgres with βexitβ then run:
python3 manage.py test app
The error will be fixed and your test will pass
Found 1 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.
----------------------------------------------------------------------
Ran 1 test in 0.003s
OK
Destroying test database for alias 'default'...
- [Django]-Where is a good place to work on accounts/profile in Django with the Django registration app?
- [Django]-What is related_name used for?
- [Django]-Django β No module named _sqlite3
1
A superuser account is the easiest way to guarantee smooth testing. so a simpler way of making the django
user su is to do ALTER django WITH SUPERUSER
.
for more information https://www.postgresql.org/docs/current/sql-alteruser.html
- [Django]-Django Rest Framework pagination extremely slow count
- [Django]-Missing Table When Running Django Unittest with Sqlite3
- [Django]-Django β Rotating File Handler stuck when file is equal to maxBytes
1
Check the the actions in runtime and switch your database
import sys
TESTING = sys.argv[1:2] == ['test']
if TESTING==False:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': config('DB_NAME'),
'USER': config('DB_USER'),
'PASSWORD': config('DB_PASSWORD'),
'HOST': config('DB_HOST'),
'PORT': ''
}
}
else:
DATABASES = {
'default': {
"ENGINE": "django.db.backends.sqlite3",
"TEST": {
"NAME": os.path.join(BASE_DIR, "test_db.sqlite3"),
}
}}
- [Django]-Django testing: Test the initial value of a form field
- [Django]-How do you detect a new instance of the model in Django's model.save()
- [Django]-Parsing unicode input using python json.loads
0
Maybe you put your test in suspended mode or as a backgrounded job. Try with fg
command in bash shell.
- [Django]-Django related_name for field clashes
- [Django]-How to loop over form field choices and display associated model instance fields
- [Django]-Default filter in Django model
0
You can also just create a test database manually but putting the TEST key in your settings.py
Then fill pass the MIRROR key with value βdefaultβ so you can test off of an exact replica of your default db, or whichever db you like.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'finance',
'USER': 'django',
'TEST': {
'MIRROR': 'default',
},
'PASSWORD': 'mydb123',
'HOST': '127.0.0.1',
'PORT': '',
}
}
You can also check the advanced django postgres docs
- [Django]-Django rest framework lookup_field through OneToOneField
- [Django]-Where can I find the error logs of nginx, using FastCGI and Django?
- [Django]-In Django 1.4, do Form.has_changed() and Form.changed_data, which are undocumented, work as expected?
0
I am facing the same issue for
pytest
I altered the django user permission so that it can create a test database while running python test cases.
ALTER USER USERNAME CREATEDB;
- [Django]-How can I chain Django's "in" and "iexact" queryset field lookups?
- [Django]-Ignoring Django Migrations in pyproject.toml file for Black formatter
- [Django]-Get the list of checkbox post in django views
0
As the error suggests the user does not have sufficient permissions to create a database. Django creates a separate database during tests so that our original development database is not affected.
We might need to give the permissions via Postgres shell.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': config('DB_NAME'),
'USER': "myuser",
'PASSWORD': config('DB_PASSWORD'),
'HOST': config('DB_HOST'),
'PORT': config('DB_PORT'),
'TEST': {
'NAME': 'test_nofoobar',
},
}
}
As the settings suggest, the name of the database user is myuser in this case. So, letβs start the Postgres shell and give database creation permission.
sudo -u postgres psql
psql (14.5 (Ubuntu 14.5-1.pgdg22.04+1))
postgres=# ALTER ROLE myuser CREATEDB;
Output: ALTER ROLE
- [Django]-Django: Arbitrary number of unnamed urls.py parameters
- [Django]-Get count of related model efficiently in Django
- [Django]-Django aggregate or annotate