Php установить модуль postgresql

Connecting to PostgreSQL using PHP

SUMMARY: This article reviews the steps necessary for connecting to a PostgreSQL database using PHP.

2. Connecting to the PostgreSQL from PHP

a. PHP command line interface

Note: although the steps in this post refer to the EnterpriseDB Advanced server, the same steps work for PostgreSQL community version as well.

Installing PHP

Before you can begin developing PHP scripts that interact with a PostgreSQL database, first you need to confirm if PHP is installed on your machine, and second you will need to confirm PostgreSQL support in your PHP installation.

To confirm if PHP is installed on your Linux machine, you can use the following command:

[root@kubemaster ~]# rpm -qa | grep php Php-json-7.3.11-1.el7.remi.x86_64 php-opcache-7.3.11-1.el7.remi.x86_64 php-common-7.3.11-1.el7.remi.x86_64 php-gd-7.3.11-1.el7.remi.x86_64 php-pdo-7.3.11-1.el7.remi.x86_64 php-mysqlnd-7.3.11-1.el7.remi.x86_64 php-pecl-mcrypt-1.0.3-1.el7.remi.7.3.x86_64 php-7.3.11-1.el7.remi.x86_64 Php-cli-7.3.11-1.el7.remi.x86_64

If PHP is not installed, then you can refer to the PHP installation instructions available here: https://www.linuxtechi.com/install-php-7-centos-7-rhel-7-server/

If you’re not sure whether your existing PHP installation already has PostgreSQL support, create a simple script named phpinfo.php (which should be placed in your web server’s document root, i.e., /var/www/html/), containing the following line:

Check the output of this script in your web browser. If PostgreSQL support has already been included, the output will contain a section similar to the following:

Note: In this document “web server” refers to an Apache web server whose document root is /var/www/html.

Connecting to the PostgreSQL server from PHP

All interactions with the PostgreSQL database are performed through the PostgreSQL extension, which is a comprehensive set of PHP functions. For a complete list of functions and information about what they do, refer to the PHP Manual instructions: http://www.php.net/manual/ref.pgsql.php.

There are two ways we can connect to the PostgreSQL database:

  1. Using the PHP command line interface.
  2. Using PHP API.

PHP command line Interface

Using the functions below we can connect to the PostgreSQL database:

[root@localhost bin]#cd /usr/bin/ [root@localhost bin]# ./php -a
php > pg_connect("host=localhost dbname=edb user=enterprisedb password=postgres"); php > pg_query("create table test(id integer)"); php > exit
edb=# \dt List of relations Schema | Name | Type | Owner --------+------+-------+-------------- public | test | table | enterprisedb (1 row) edb=# \d+ test Table "public.test" Column | Type | Collation | Nullable | Default | Storage | Stats target | Description --------+---------+-----------+----------+---------+---------+--------------+------------- id | integer | | | | plain | |

PHP API

A simple PHP script that opens a connection to a PostgreSQL database, create a table would look something like this:

[root@localhost bin]#cd /var/www/html [root@localhost bin]#cd /var/www/html [root@localhost bin]#vi test.php

The above test.php file needs to be executed from the browser at http://localhost/test.php:

As you can see, interacting with the database from within PHP is fairly straightforward using pg_connect(). If the connection attempt fails, the pg_connect() function will return false. Failed connection attempts can, thus, be detected by testing the return value:

[root@localhost bin]#cd /var/www/html [root@localhost bin]#vi connection.php else < echo 'Connection attempt failed.'; >pg_close($db_handle); ?>

Execute the connection.php script from the browser at http://localhost/connection.php:

The PHP script below is a single script you can use to perform PHP connection testing, query status checking, and data fetching from the database.

======================================== [root@localhost bin]#cd /var/www/html [root@localhost bin]#vi connection_test.php else < echo 'Connection attempt failed.'; >echo "

Connection Information

"; echo "DATABASE NAME:" . pg_dbname($db_handle) . "
"; echo "HOSTNAME: " . pg_host($db_handle) . "
"; echo "PORT: " . pg_port($db_handle) . "
"; echo "

Checking the query status

"; $query = "SELECT fname,lname FROM person"; $result = pg_exec($db_handle, $query); if ($result) < echo "The query executed successfully.
"; echo "

Print First and last name:

"; for ($row = 0; $row < pg_numrows($result); $row++) < $firstname = pg_result($result, $row, 'fname'); echo $firstname ." "; $lastname = pg_result($result, $row, 'lname'); echo $lastname ."
"; > > else < echo "The query failed with the following error:
"; echo pg_errormessage($db_handle); > pg_close($db_handle); ?>

Источник

Installing/Configuring

This happened to me also with PHP 5.4.1
Copying the offending DLL everywhere didn’t worket, and I don’t have Postgres installed in the server, but also planned to use PHP against different Postgres versions, so the only solution I found that worked was to put in httpd.conf a line like this:

LoadFile «C:/Program Files/PostgreSQL/8.4/bin/libpq.dll»

but refering to the libpq.dll that comes bundled with PHP, like this:

After that it worked fine to me.

If you get a message like this:
«Php Startup: Unable to load dynamic library ‘C:\php\ext\php_pgsql.dll»

1. In the Configuration File alter the order of the libraries «php_exif.dll» and «php_mbstring.dll», in such a way that is called first the library «php_mbstring.dll»

2. Copy all the libraries in the folder «bin» of your installation of PostgreSQL and paste them into the root folder of Windows

3. Uncomment the line «extension=php_pgsql.dll»
4. Now you should see on the page generated by «phpinfo()» section of PostgreSQL

Unable to load dynamic library ‘C:\\php\\ext\\php_pgsql.dll’ — The specified module could not be found.

copy libpq.dll/all files from zip (if you have php from installer)

#BEGIN PHP INSTALLER EDITS — REMOVE ONLY ON UNINSTALL
PHPIniDir «c:/PHP/»
Loadfile «C:\php\php5ts.dll»
Loadfile «C:\php\libpq.dll»
LoadModule php5_module «c:/PHP/php5apache2_2.dll»
#END PHP INSTALLER EDITS — REMOVE ONLY ON UNINSTALL
AddHandler application/x-httpd-php .php
AddHandler application/x-httpd-php-source .phps

(in php.ini default: extension_dir =»c:\PHP\ext» )

if it doesn’t work add c:\php (php path) to your windows path (and restart)

tested on php5.2.8 + apache2.2.3

If you get this error on Windows:

Unable to load dynamic library ‘C:/php/ext\\php_pgsql.dll’ — The specified module could not be found.

Try downloading the PostgreSQL binaries-no-install archive from http://www.postgresql.org/ftp/binary/v8.3.5/win32/ (replace 8.3.5 with the latest stable version) and copy these DLLs to C:/WINDOWS/system32/ :

Another windows xp solution for «Unable to load dynamic library ‘php_pgsql.dll’ — The specified module could not be found.»

Add the path of the postgres binaries to the system search-path:

Right click on «My Computer», select «properties», «advanced»-tab, «Environment variables»-button, «System variables»-section, select «path» and click «Edit» and add the path to the postgres binaries at the end, in my case it was «C:\Program Files\PostgreSQL\8.4\bin» and then restart the computer (or at least restart the apache/php processes)

Please note that the system-path can have several values, each separated by a semicolon «;».

Источник

How To Install php8.1-pgsql on Ubuntu 22.04

In this tutorial we learn how to install php8.1-pgsql on Ubuntu 22.04.

What is php8.1-pgsql

This package provides the PostgreSQL module(s) for PHP.

PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used open source general-purpose scripting language that is especially suited for web development and can be embedded into HTML.

There are three ways to install php8.1-pgsql on Ubuntu 22.04. We can use apt-get , apt and aptitude . In the following sections we will describe each method. You can choose one of them.

Install php8.1-pgsql Using apt-get

Update apt database with apt-get using the following command.

After updating apt database, We can install php8.1-pgsql using apt-get by running the following command:

sudo apt-get -y install php8.1-pgsql 

Install php8.1-pgsql Using apt

Update apt database with apt using the following command.

After updating apt database, We can install php8.1-pgsql using apt by running the following command:

sudo apt -y install php8.1-pgsql 

Install php8.1-pgsql Using aptitude

If you want to follow this method, you might need to install aptitude first since aptitude is usually not installed by default on Ubuntu. Update apt database with aptitude using the following command.

After updating apt database, We can install php8.1-pgsql using aptitude by running the following command:

sudo aptitude -y install php8.1-pgsql 

How To Uninstall php8.1-pgsql on Ubuntu 22.04

To uninstall only the php8.1-pgsql package we can use the following command:

sudo apt-get remove php8.1-pgsql 

Uninstall php8.1-pgsql And Its Dependencies

To uninstall php8.1-pgsql and its dependencies that are no longer needed by Ubuntu 22.04, we can use the command below:

sudo apt-get -y autoremove php8.1-pgsql 

Remove php8.1-pgsql Configurations and Data

To remove php8.1-pgsql configuration and data from Ubuntu 22.04 we can use the following command:

sudo apt-get -y purge php8.1-pgsql 

Remove php8.1-pgsql configuration, data, and all of its dependencies

We can use the following command to remove php8.1-pgsql configurations, data and all of its dependencies, we can use the following command:

sudo apt-get -y autoremove --purge php8.1-pgsql 

References

Summary

In this tutorial we learn how to install php8.1-pgsql package on Ubuntu 22.04 using different package management tools: apt, apt-get and aptitude.

Источник

Читайте также:  Php parse json files
Оцените статью