Question: I have a complicated query and since I need that my module work on both mysql and postgres, I need to write two version of it.

Unfortunately, I don't know how I can check if the db I use is mysql or postgres, to know which query use. Do you know if a function can return this value?

Answer: 
As @kordirko says, one option is to query the server version: `SELECT version();` will work on both MySQL and PostgreSQL, though not [most](http://sqlfiddle.com/#!4/d41d8/14664) [other database](http://sqlfiddle.com/#!6/d41d8/5941) [engines](http://sqlfiddle.com/#!5/d41d8/599).

Parsing version strings is always a bit fragile though, and MySQL returns just a version number like `5.5.32` wheras PostgreSQL returns something like `PostgreSQL 9.4devel on x86_64-unknown-linux-gnu, compiled by gcc (GCC) 4.7.2 20121109 (Red Hat 4.7.2-8), 64-bit`. What do you do if you're connecting to a PostgreSQL-*compatible* database like EnterpriseDB Postgres Plus, or a MySQL-*compatible* database?

It's much safer to use [the Drupal function for the purpose, `DatabaseConnection::databaseType`](https://api.drupal.org/api/drupal/includes!database!database.inc/function/DatabaseConnection%3A%3AdatabaseType/7). This avoids a query round-trip to the DB, will work on databases that won't understand/accept `SELECT version()`, and will avoid the need to parse version strings.

You'll find [this bug report](https://drupal.org/node/1353580) useful; it suggests that the correct usage is `Database::getConnection()->databaseType()`.

(I've never even used Drupal, I just searched for this).
As long as the abstract [DatabaseConnection](https://api.drupal.org/api/drupal/includes%21database%21database.inc/class/DatabaseConnection/7) class extends PDO class, you can invoking pdo methods in order to know the current database driver.

For instance:

```
$conn = Database::getConnection();
print $conn->getAttribute($conn::ATTR_DRIVER_NAME); #returns mysql, pgsql...

```

There is a second way to do it using [DatabaseConnection::driver()](https://api.drupal.org/api/drupal/includes!database!database.inc/function/DatabaseConnection%3A%3Adriver/7):

```
print $conn->driver();

```

or [DatabaseConnection::databaseType()](https://api.drupal.org/api/drupal/includes!database!database.inc/function/DatabaseConnection%3A%3Adriver/7);

```
print $conn->databaseType();

```

Note that DatabaseConnection::driver() and DatabaseConnection::databaseType() are similar functions but not equals!
The return value from DatabaseConnection::driver() method depends on the implementation and other factors. 
in the Drupal Database API page:

*database.inc abstract public DatabaseConnection::driver()*

> 
> This is not necessarily the same as the type of the database itself. For instance, there could be two MySQL drivers, mysql and mysql\_mock. This function would return different values for each, but both would return "mysql" for [databaseType()](https://api.drupal.org/api/drupal/includes!database!database.inc/function/DatabaseConnection%3A%3Adriver/7).
> 
> 
> 

In the most cases you just gonna want to use only
`$conn->getAttribute($conn::ATTR_DRIVER_NAME)` 
or `$conn->databaseType()`

If you want get more specific properties, you should take advantage the PHP ReflectionClass features:

```
$conn = Database::getConnection();
$ref = new ReflectionClass($conn);
#ref->getProperties, ref->getConstants $ref->isAbstract...

```

Reference:  

[PDO::getAttribute](http://www.php.net/manual/en/pdo.getattribute.php)  

[PDO::ATTR\_DRIVER\_NAME](http://www.php.net/manual/en/pdo.constants.php)  

[Drupal Database API](https://drupal.org/node/310069)  

[Drupal Base Database API class](https://api.drupal.org/api/drupal/includes!database!database.inc/class/DatabaseConnection/7)