Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find Postgres version from Django?

I want to know version of the default Postgres database. How to find it using Django?

like image 763
Max Malysh Avatar asked Nov 10 '17 23:11

Max Malysh


1 Answers

Get a database connection:

from django.db import connection

And access the inner psycopg2 connection object:

print(connection.cursor().connection.server_version)

One-liner:

$ python3 manage.py shell -c "from django.db import connection; print(connection.cursor().connection.server_version)"
90504

The number is formed by converting the major, minor, and revision numbers into two-decimal-digit numbers and appending them together. For example, version 8.1.5 will be returned as 80105.

Docs: http://initd.org/psycopg/docs/connection.html#connection.server_version

like image 98
Max Malysh Avatar answered Sep 27 '22 20:09

Max Malysh