modius
modius

Reputation: 2063

How can I confirm a database is Postgres & what version it is using SQL?

I'm building an installer for an application. The user gets to select a datasource they have configured and nominate what type of database it is. I want to confirm that the database type is indeed Postgres, and if possible, what version of Postgres they are running by sending a SQL statement to the datasource.

Upvotes: 5

Views: 1159

Answers (6)

Learning
Learning

Reputation: 8175

Interesting ... version() is a function! I wonder why? Version is not going to change or return different values under different inputs/circumstances.

Curious because I remember from old days that in Sybase it used to be a global variable and version could be found out by doing "select @@version"

Upvotes: 0

Milen A. Radev
Milen A. Radev

Reputation: 62563

SHOW server_version;

(for completeness)

Upvotes: 4

Vinko Vrsalovic
Vinko Vrsalovic

Reputation: 340151

This is DB dependent, and in case this function exists in another dbms, this says PostgreSQL in the output

select version()

Upvotes: 2

Neall
Neall

Reputation: 27114

PostgreSQL has a version() function you can call.

SELECT version();

It will return something like this:

                                            version
-----------------------------------------------------------------------------------------------
PostgreSQL 8.3.3 on i486-pc-linux-gnu, compiled by GCC cc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)

Upvotes: 2

Matthias Kestenholz
Matthias Kestenholz

Reputation: 3348

Try this:

mk=# SELECT version();
                                            version                                            
-----------------------------------------------------------------------------------------------
 PostgreSQL 8.3.3 on i486-pc-linux-gnu, compiled by GCC cc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
(1 row)

The command works too in MySQL:

mysql> select version();
+--------------------------------+
| version()                      |
+--------------------------------+
| 5.0.32-Debian_7etch1~bpo.1-log | 
+--------------------------------+
1 row in set (0.01 sec)

There is no version command in sqlite as far as I can see.

Upvotes: 4

Galwegian
Galwegian

Reputation: 42227

SELECT version();

Upvotes: 2

Related Questions