How to find server_version in psycopg3?

I have this test in Django [1]:

from django.db import connection

def test_postgresql_version(self):
    postgresql_version = connection.cursor().connection.server_version
    if (postgresql_version >= 140000):
        pass
    else:
        raise NotImplementedError("postgresql version must be at least 14.0.")

This test fails with psycopg3:

AttributeError: 'Connection' object has no attribute 'server_version'

How do I check the server_version in psycopg3?

In Psycopg 3 (used with Django 4+ when psycopg is installed instead of psycopg2), the server_version attribute is no longer available directly on connection.cursor().connection. Instead, you should use the info object on the connection.

Modify your test to use connection.info.server_version:

from django.db import connection

def test_postgresql_version(self):
    postgresql_version = connection.connection.info.server_version 
    if postgresql_version >= 140000:
        pass
    else:
        raise NotImplementedError("PostgreSQL version must be at least 14.0.")
Вернуться на верх