PostgreSQL PHP Connection

Connection to PostgreSQL for PHP Applications

Follow the instruction below to learn how to connect your PHP application, hosted within Apiqcloud Cloud, to the PostgreSQL database server.

Create Environment

1. Log into the Apiqcloud.

2. Create PHP environment with PostgreSQL database:
    
3. Check your e-mail - your inbox should have a message with database login and password:

                                    

Now, you can upload your PHP application package and deploy it to the environment.

Database Connection

1. Click Config button for your Apache server.

       

2. Navigate to etc folder and open php.ini file.

3. Add extension=pgsql.so line after extension=mysql.so, like it is shown in the image below:

        

4. Save the changes and Restart nodes for Apache server.

        

5. There are 2 main PG functions for connection:
    • Connection to your postgresql database:
pg_connect ("host=host_address port=5432 dbname=postgres user=webadmin password=password");
  • host - Postgres server address that you received by email (note that it must be without http://): 
    postgres{node_id}-{your_env_name}.{hoster_domain}
  • port - by default 5432
  • dbname - name of your database
  • user - by default is "webadmin"
  • password - the password that you received via email
  • Closing connection to postgresql database: pg_close()

All Postgres functions you can see here.

6. You need to write necessary functions in every *.php page, which should be connected to the database.

Connection Check Up

  • check the connection using this code:
 
1
2
3
4
5
6
7
8
9
10
11
<?php
 
$dbconn = pg_connect("host=postgres-demo.jelastic.com port=5432 dbname=postgres-demo user=webadmin password=password");
//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);
?>
  • execute simple request and output it into table:
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
 
$conn = pg_connect("host=postgres-demo.jelastic.com port=5432 dbname=postgres-demo user=webadmin password=password");
if (!$conn) {
 echo "An error occured.\n";
 exit;
}
 
$result = pg_query($conn, "SELECT * FROM test_table");
if (!$result) {
 echo "An error occured.\n";
 exit;
}
 
while ($row = pg_fetch_row($result)) {
 echo "value1: $row[0]  value2: $row[1]";
 echo "<br />\n";
}
 
?>

Now, you can use connection to PostgreSQL database for your PHP application.

  • PostgreSQL PHP Connection
  • 6 Utilisateurs l'ont trouvée utile
Cette réponse était-elle pertinente?

Articles connexes

How to Enable PHP Extensions

1.Click on config button for your app server.  2. Within the opened configuration tab, click...

Deploy PHP Project Via GIT SVN

Deploy PHP Project via GIT/SVN You can host any public or your private PHP project at...

How to Check Change PHP Version in Apiqcloud

How to check PHP Version in Apiqcloud:   After making new Environment click on “Change...

Ion cube Loader

Running Encoded PHP Scripts with ionCube Loader Encoding application sources is a common...

MariaDB PHP connection

Connection to MySQL/MariaDB for PHP MySQL and MariaDB are highly popular open-source...