Friday, December 31, 2010

Using PostgreSQL with Symfony 1.4

Recently my company decided to upgrade from Symfony 1.0 to Symfony 1.4.  This upgrade was long overdue.  With this upgrade came a problem; the PDO driver for MySQL did not support an SSL connection, which is something that one of our clients required.  As a result we decided to go with PostgreSQL as our database, as the PDO driver did support an SSL connection.

After doing several Google searches and asking for help on some IRC forums, it became clear that very few people knew much about getting Symfony to work with PostgreSQL.  Not surprising since almost all PHP developers use MySQL.  Turns out, it is not as bad as it sounds to get Symfony up and running with PostgreSQL quickly.

Step 1: Install PostgreSQL.  Quick instructions for this can be found on PostgreSQL's own website: http://www.postgresql.org/docs/8.4/interactive/install-short.html. The only thing I would change about this is adding the --with-openssl option when you run the configure script before compiling.

Step 2: Start your Symfony project.  This is also pretty straight forward, the instructions for this are here: http://www.symfony-project.org/jobeet/1_4/Doctrine/en/01

Step 3: Download the Symfony plugin for PostgreSQL.
php ./symfony plugin:install sfPostgresDoctrinePlugin

Step 4: Publish the plugin.
php ./symfony plugin:publish-config

Step 5: Alter the config/ProjectConfiguration.class.php file to get the enablePlugins function to accept an array.

class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
$this->enablePlugins(array(
'sfDoctrinePlugin',
'sfPostgresDoctrinePlugin'));
}
}


Step 6: Create the plpgsql language.  As the postgresql user run the command:
createlang plpgsql [databasename]

And there you have it!  You should now be able to operate Symfony with PostgreSQL just like you did with MySQL.  Enjoy!