User Rating: 3 / 5

Star ActiveStar ActiveStar ActiveStar InactiveStar Inactive

Trying to use Joomla! on a non default PostgreSQL database port?

The problem was that the default PostgreSQL Driver was not taken into account a port in the connection string. It only used the default port 5432 when connecting to the PostgreSQL database server. I updated the PostgresqlDriver to support non default port 5432. The same principal as the MySQLi driver is used to get the database hostname and port from the host connection string (databasehost:port)

Have a look at this: https://github.com/joomla/joomla-framework/pull/141

Change the file 'Joomla/Database/Postgresql/PostgresqlDriver.php'


/**
* Part of the Joomla Framework Database Package
*
* @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/

namespace Joomla\Database\Postgresql;

use Psr\Log;
use Joomla\Database\DatabaseDriver;

/**
* PostgreSQL database driver
*
* @since 1.0
*/
class PostgresqlDriver extends DatabaseDriver
{
/**
* The database driver name
*
* @var string
*/
public $name = 'postgresql';

/**
* Quote for named objects
*
* @var string
*/
protected $nameQuote = '"';

/**
* The null/zero date string
*
* @var string
*/
protected $nullDate = '1970-01-01 00:00:00';

/**
* @var string The minimum supported database version.
* @since 1.0
*/
protected static $dbMinimum = '8.3.18';

/**
* Operator used for concatenation
*
* @var string
* @since 1.0
*/
protected $concat_operator = '||';

/**
* Query object returned by getQuery
*
* @var \Joomla\Database\Postgresql\PostgresqlQuery
* @since 1.0
*/
protected $queryObject = null;

/**
* Database object constructor
*
* @param array $options List of options used to configure the connection
*
* @since 1.0
*/
public function __construct( $options )
{
$options['host'] = (isset($options['host'])) ? $options['host'] : 'localhost';
$options['user'] = (isset($options['user'])) ? $options['user'] : '';
$options['password'] = (isset($options['password'])) ? $options['password'] : '';
$options['database'] = (isset($options['database'])) ? $options['database'] : '';
$options['port'] = (isset($options['port'])) ? $options['port'] : null;

// Finalize initialization
parent::__construct($options);
}

/**
* Database object destructor
*
* @since 1.0
*/
public function __destruct()
{
if (is_resource($this->connection))
{
pg_close($this->connection);
}
}

/**
* Connects to the database if needed.
*
* @return void Returns void if the database connected successfully.
*
* @since 1.0
* @throws \RuntimeException
*/
public function connect()
{
if ($this->connection)
{
return;
}

// Make sure the postgresql extension for PHP is installed and enabled.
if (!function_exists('pg_connect'))
{
throw new \RuntimeException('PHP extension pg_connect is not available.');
}
/*
* pg_connect() takes the port as separate argument. Therefore, we
* have to extract it from the host string (if povided).
*/

// Check for empty port
if (!($this->options['port']))
{
// Port is empty or not set via options, check for port annotation (:) in the host string
$tmp = substr(strstr($this->options['host'], ':'), 1);

if (!empty($tmp))
{
// Get the port number
if (is_numeric($tmp))
{
$this->options['port'] = $tmp;
}

// Extract the host name
$this->options['host'] = substr($this->options['host'], 0, strlen($this->options['host']) - (strlen($tmp) + 1));

// This will take care of the following notation: ":5432"
if ($this->options['host'] == '')
{
$this->options['host'] = 'localhost';
}
}
// No port annotation (:) found, setting port to default PostgreSQL port 5432
else
{
$this->options['port'] = '5432';
}
}

// Build the DSN for the connection.
$dsn = "host={$this->options['host']} port={$this->options['port']} dbname={$this->options['database']} user={$this->options['user']} password={$this->options['password']}";