How Tos > MySQL and Databases

Install and run PostgreSQL

MAMP PRO does not include an instance of PostgreSQL, however it does include the necessary PDO extension to connect with PostgreSQL. Using phpInfo you can confirm the current version of PHP you are using in MAMP PRO has the PDO PostgreSQL extension installed.

MAMP

Download the PostgreSQL database for Mac and install the postgresql-osx.dmg file. You can download PostgreSQL here.

MAMP

Your PostgreSQL database will run on port 5432.

MAMP

After installation, by default, you will have a postgres icon on your menu bar. Open the PostgreSQL browser.

MAMP

Create a database. (For purposes of the test script below, a “MyPostGresDB” should be created)

MAMP

Here is a simple script for connecting to the PostgreSQL “MyPostGresDB” database. In MAMP PRO, create a host. Add the following script to your index.php file. Make sure you use your own password, and not “pass”.

<?php
  try {
    $dbh = new PDO('pgsql:host=localhost;port=5432;dbname=MyPostGresDB;user=postgres;password=pass');
    echo "PDO connection object created";
  }
  catch(PDOException $e) {
    echo $e->getMessage();
  }
?>

When you open this host in your browser you should set the “connection object created” message displayed.