简体   繁体   中英

Database config for Django testing

I am building an app using Django and Postgres. I managed to do migrations and I want to test it. When I test with sqlite everything works fine, but when I run tests with postgres I'm getting this error:

Creating test database for alias 'default'...
Got an error creating the test database: permission denied to create database

I've checked user's permissions and I'm sure that this user have permission to create database.

My database config looks like this:

# DATABASES = {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#     }
# }

DATABASES = {
     'default': {
         'ENGINE': 'django.db.backends.postgresql_psycopg2',
         'NAME': '***',
         'USER': '***',
         'PASSWORD': '***',
         'HOST': '****',
         'PORT': '****',
     }
 }

My postgres db is on a server.

My questions are:

  • What is the right way to config my db and run tests?
  • Should I be using sqlite for testing?
  • If so how my code should look like, so I don't have to comment configs?

It looks like your DB user doesn't have permission to create a new database. Please, take a look here . This command-line utility allows you to create a user and set their permissions.

Example:

createuser my_user --createdb -W --username postgres

Note : you are creating user "my_user" on behalf of PostgreSQL admin role which is postgres by default.

Answering your questions:

  1. You may have several configs for different stages, eg development, testing, production.
  2. You could use both SQLite and Postgres databases for testing purposes to some extent. You should be awarded, though, if your app relies on some specific features available only in Postgres, then using SQLite for testing doesn't make sense. I personally prefer using the same database for all stages. You could also use docker if you don't want to install DB server on your machine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM