I have the follow statements:
from alembic import op
conn = op.get_bind()
Now I want to get postgresql version.
According to documentation it is server_version property of connection:
conn = psycopg2.connect(settings.DB_DSN)
>>> conn.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.
If you can run raw SQL queries, just run the following:
SELECT version();
show server_version; or select current_setting('server_version'); to get just a version number without details.If you are using ubuntu then you can execute command
psql -V
to check postgresql version where
V must be in capital.
and to check version of psycopg2 you can run following command
pip freeze | grep psycopg2
alternatively you can execute
pip freeze
to show all the installed packages with their respective version
import os
import psycopg2
conn_str = {
'user': os.environ['USERNAME'].lower(),
'host': 'POSTGES_SERVER_NAME',
'port': 5432,
'database': 'database_name'
}
conn = psycopg2.connect(**conn_str)
cursor = conn.cursor()
cursor.execute('SELECT VERSION()')
row = cursor.fetchone()
print(row)
cursor.close()
# output:
# ('PostgreSQL 10.1, compiled by Visual C++ build 1800, 64-bit',)