Follow the instruction below to learn how to connect your PHP application, hosted within CirrusGrid PaaS, to the PostgreSQL database server:
1. Log into CirrusGrid dashboard.
2. Create an environment with the PHP application server (e.g. Apache PHP) and the PostgreSQL database.
3. Check your email inbox for a message with database credentials (login and password).
Now, you can access your database via web admin panel and connect it to your PHP application.
1. Click the Config button for your Apache server.
2. Navigate to the etc folder and open the php.ini file.
Add the extension=pgsql.so line like it is shown in the image below.
3. Save the changes and Restart nodes for your Apache server(s).
4. There are two main PG functions for operating with a database server:
pg_connect(“host={host} port={port} dbname={dbname} user={user} password={password}“); where:
5. You need to write the necessary functions in every *.php page, which should be connected to the database.
<?php $dbconn = pg_connect("host=postgres.jelastic.com port=5432 dbname=postgres user=webadmin password=passw0rd"); //connect to a database named "postgres" on the host "host" with a username and password if (!$dbconn){ echo "<center><h1>Doesn't work =(</h1></center>"; }else echo "<center><h1>Good connection</h1></center>"; pg_close($dbconn); ?>
<?php $conn = pg_connect("host=postgres.jelastic.com port=5432 dbname=postgres user=webadmin password=passw0rd"); if (!$conn) { echo "An error occurred.\n"; exit; } $result = pg_query($conn, "SELECT * FROM test_table"); if (!$result) { echo "An error occurred.\n"; exit; } while ($row = pg_fetch_row($result)) { echo "value1: $row[0] value2: $row[1]"; echo "<br />\n"; } ?>
You can use the above-described examples to create your own PHP application, which utilizes connection to the PostgreSQL database.
Powered by BetterDocs
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.