5

I have the follow statements:

from alembic import op
conn = op.get_bind()

Now I want to get postgresql version.

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

Comments

0

If you can run raw SQL queries, just run the following:

SELECT version();

1 Comment

There is also show server_version; or select current_setting('server_version'); to get just a version number without details.
0

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

Comments

0
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',)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.