postgres.php 1002 Bytes
Newer Older
1 2 3 4 5
<?php namespace Laravel\Database\Connectors; use PDO;

class Postgres extends Connector {

	/**
6
	 * Establish a PDO database connection.
7 8 9 10 11 12
	 *
	 * @param  array  $config
	 * @return PDO
	 */
	public function connect($config)
	{
13
		extract($config);
14

15
		$dsn = "pgsql:host={$host};dbname={$database}";
16

Taylor Otwell committed
17 18 19 20
		// The developer has the freedom of specifying a port for the PostgresSQL
		// database or the default port (5432) will be used by PDO to create the
		// connection to the database for the developer.
		if (isset($config['port']))
21
		{
Taylor Otwell committed
22
			$dsn .= ";port={$config['port']}";
23 24 25 26
		}

		$connection = new PDO($dsn, $username, $password, $this->options($config));

Taylor Otwell committed
27 28 29
		// If a character set has been specified, we'll execute a query against
		// the database to set the correct character set. By default, this is
		// set to UTF-8 which should be fine for most scenarios.
30 31
		if (isset($config['charset']))
		{
32
			$connection->prepare("SET NAMES '{$config['charset']}'")->execute();
33 34
		}

35
		return $connection;
36 37 38
	}

}