pythondjangopostgresqlpsycopg2psycopg3

How to find server_version in psycopg3?


I have this test in Django [1] which is using psycopg2:

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?


Solution

  • Eventually we used the following test:

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

    This used connection.info.server_version from Ankur's answer.