如何从 Django 中找到 Postgres 版本?

Max*_*ysh 6 django postgresql psycopg2 django-orm

我想知道默认 Postgres 数据库的版本。如何使用 Django 找到它?

Max*_*ysh 7

获取数据库连接:

from django.db import connection
Run Code Online (Sandbox Code Playgroud)

并访问内部 psycopg2 连接对象:

print(connection.cursor().connection.server_version)
Run Code Online (Sandbox Code Playgroud)

单线:

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

该编号是通过将主要编号、次要编号和修订编号转换为两位十进制数字并将它们附加在一起而形成的。例如,版本 8.1.5 将返回为 80105。

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