Problems running tests with postgresql

Hello all,

I hope you can help me further.

I try to lern Django while going to the tutorial step by step. But now I’m stuck in the tutorial part 5 automated tests.
If I run

python manage.py test polls

I get the error that the user cannot create databases.

But in postgresql I granted my testuser the CREATE DATABASE command.

 Role name  |  Attributes | Member of 
------------+-------------+--------------
 testuser   |  Create DB  | {}

I started a dbshell after setting the PATH variable:

PYTHONPATH=$PYTHONPATH:$(pwd)

I can start a dbshell

python manage.py dbshell

With my database settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'OPTIONS': {
            'service': 'my_service',
            'passfile': '.my_pgpass',
        },
        'TEST': {'NAME': 'test'}
      }
}

My .pg_service.conf:


[my_service]
host=127.0.0.1
user=testuser
dbname=testdb
port=5432

My .my_pgpass:

127.0.0.1:5432:testdb:testuser:testpassword

and within the dbshell the testuser can create and drop databases without any errors. All other operations (make migrations) also perform well without problems. If I create the test database within postgresql and then run

python manage.py test polls --keepdb

it complaints the database schema is restricted. I use the following versions:

Python 3.10.9
Django 4.1.7
Postgresql 15.1

My tests.py:

 import datetime
 
 from django.test import TestCase
 from django.utils import timezone
 
 from .models import Question
 
 class QuestionModelTests(TestCase):
     
 
 import datetime
 
 from django.test import TestCase
 from django.utils import timezone
 
 from .models import Question
 
 class QuestionModelTests(TestCase):
     
     def test_was_published_recently_with_future_question(self):
         """was_published_recently() returns False for questions whose pub_date is in the future"""
         time = timezone.now() + datetime.timedelta(days=30)
         future_question = Question(pub_date=time)
         self.assertIs(future_question.was_published_recently(), False)

I hope someone could help me as I tried all I can.

Many thanks

I can’t tell for sure, but there are a couple things I’d check.

Your .my_pgpass file doesn’t show credentials for the testuser account to log on to the test database.

Also, see this for details about how the default permissions have changed in PostgreSQL 15:

(And see the link to the SO post on that page that addresses a test-related issue.)

Many thanks for your input. I very appreciated it.

I have changed my .my_pgpass to:

127.0.0.1:5432:testdb:testuser:testpassword
127.0.0.1:5432:test:testuser:testpassword

I then I did another try with changed database settings:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'OPTIONS': {
            'options': '-c search_path=testschema',
            'service': 'my_service',
            'passfile': '.my_pgpass',
        },
        'TEST': {'NAME': 'test'}
      }
}

Before I have created a Schema named testschema in each database (testdb and test) according to the description in your link. Then I ran the command python manage.py test polls --keepdb but then I have the error no schema chosen for creating objects.

But it looks like there have been testing issues with Django and Postgresql since 2016 without any satisfying solution.

I think I will change the backend to MariaDB and going through the tutorial again…

Did you follow all the steps in the referenced link? (Specifically the CREATE SCHEMA statement.)

It works for me if I do everything as documented. (This includes creating the schema for both the primary and test databases.)

Yes I did. I even have made the alter role statements as described: I also set the schema for the primary and the test database. But when I try to run the test I still get the error:

ERROR: no schema has been selected to create in

But only for running tests. All migrations work without any issues.

To be honest I have avoided creating a specialized test runner as described here: