How to discover which postgres vesion is in use (on 1.11)?

51 views
Skip to first unread message

Hanne Moa

unread,
Jul 19, 2017, 10:15:29 AM7/19/17
to Django Users
When https://code.djangoproject.com/ticket/18332 lands (No generic way
to get database backend version) this will be trivial but I need a
solution for this now: How can I find out which version of postgres is
in use?

I need it thanks to JSONField. 9.6 has support for it but older
postgresqls don't, and I'd like to then use a third-party
json-in-TextField solution instead.

A model with a django.contrib.postgres.fields.JSONField cannot be
migrated on posgresqls older than 9.4. (We run 9.3 and I need this
project to run on >=9.3) You get 'django.db.utils.ProgrammingError:
type "jsonb" does not exist'. At that point it's a little late
swapping out the import.

--
HM

Antonis Christofides

unread,
Jul 19, 2017, 2:40:45 PM7/19/17
to django...@googlegroups.com

Hello,

You can probably do something like this:

import sys

from django.db import connection

with connection.cursor() as cursor:
    cursor.execute("SELECT version()")
    result = cursor.fetchone()[0]

sys.stdout.write(result + '\n')

This prints out something like "PostgreSQL 9.4.12 on x86_64-unknown-linux-gnu, compiled by gcc (Debian 4.9.2-10) 4.9.2, 64-bit".

The "SELECT version()" is a PostgreSQL-specific way of getting the server version. The rest is Django's way of executing raw SQL queries.

Regards,

Antonis

Antonis Christofides
http://djangodeployment.com

Tim Graham

unread,
Jul 20, 2017, 2:51:26 PM7/20/17
to Django users
You can use:

>>> from django.db import connection
>>> connection.pg_version
90507
Reply all
Reply to author
Forward
0 new messages