@arnoldo.moen
To connect PostgreSQL and PHP on a Mac, you can follow these steps:
1
|
brew install [email protected] |
Replace "7.4" with the version of PHP you are using.
1
|
php --ini |
Open the php.ini file in a text editor and uncomment the line that loads the PostgreSQL extension by removing the semicolon at the beginning of the line. It should look like this:
1
|
extension=pgsql |
Save the file and restart your PHP server.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php $host = 'localhost'; $dbname = 'mydatabase'; $user = 'myuser'; $password = 'mypassword'; try { $pdo = new PDO("pgsql:host=$host;dbname=$dbname", $user, $password); echo "Connected to the database successfully"; } catch (PDOException $e) { die("Could not connect to the database: " . $e->getMessage()); } ?> |
Replace the placeholders with your actual database information. Save the code in a PHP file and run it in your web browser or using the PHP command-line interface to test the connection.
That's it! You have successfully connected PostgreSQL and PHP on your Mac.