Connection to PostgreSQL for PHP Applications
Follow the instruction below to learn how to connect your PHP application, hosted within the platform, to the PostgreSQL database server:
Create Environment
1. Log into the platform 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.
Configure Database Connection
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:
- opening PostgreSQL connection:
pg_connect(“host={host} port={port} dbname={dbname} user={user} password={password}"); where:
- {host} - the PostgreSQL server Host (i.e. access URL without http://) that you’ve received via email, for example node171206-php-postgresql.inetsolutions.cloud
- {port} - a connection port (the default one is 5432)
- {dbname} - a name of your database
- {user} - an account name to access database with (we’ll use the default webadmin one)
- {password} - a password for the appropriate user
- closing PostgreSQL connection: pg_close()
5. You need to write the necessary functions in every *.php page, which should be connected to the database.
Connection Check Up
- check the connection using this code:
|
|
- execute simple request and output it into table:
|
|
You can use the above-described examples to create your own PHP application, which utilizes connection to the PostgreSQL database.