How to Install and Configure the php-pgsql driver for CockroachDB in Linux
The Structured Query Language (SQL) database of CockroachDB uses the php-pgsql driver to enable you to connect to and close a database, run SQL queries, and print results in HTML format. As you know, each operating system type functions differently from another. For example, Linux and Windows aren’t compatible, but CockroachDB works with them both.
This tutorial explains how to Install and Configure the PHP php-pgsql driver for CockroachDB in Linux. The steps are precise, short, and simple for installing PHP driver in Cockroach DB in Linux. Even so, it’s important to add the php-pgsql driver correctly; otherwise, what should be an easy install process can quickly become an unnecessary challenge. No worries. Here’s a relaxing way to get it all done.
Download and install PostgreSQL v8.0 or later.
Download and install CoackroachDB for Linux v19.1.1 or later.
From any shell, use the command below to do the binary extraction.
wget -qO- https://binaries.cockroachdb.com/cockroach-v19.1.1.linux-amd64.tgz | tar xvz
- In a shell, set the system PATH and be able to execute CockroachDB commands.
>NOTE: The command to extract the binary is based on v19.1.1 for Linux. If there is a later version, you would extract that version that matches your download of CockroachDB.
>In addition, if you receive an error message, use a
sudo command at the start of your script so you’ll be able to access permissions.
Update Linux’s APT repositories
Prepare for the installation by updating the APT repositories in Linux.
To do this, create a new file: /etc/apt/sources.list.d/pgdg.list
- Next, add a repository line.
>Tip: To quickly figure which Linux distribution is installed on your OS, use the
lsb_release-c command in place of
LINUX_VERSION_HERE as in the example code below. Proceed to add
-pgdg main at the end of the line as shown.
- Finally, obtain a signing key for the repository importation and package list update request.
sudo apt-get update
- Now you can go ahead perform an
apt-get installfor the PostgreSQL library.
- Run the
- Use the
systemctl status postgresqlcommand to attest that the service is running. You should see a similar result like this:
Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor prese
Active: active (exited) since Sat 2019-06-08 18:25:24 PST; 1min 9s ago
Main PID: 6077 (code=exited, status=0/SUCCESS)
Tasks: 0 (limit: 4915)
Jun 08 18:25:24 teamsolo systemd: Starting PostgreSQL RDBMS...
Jun 08 18:25:24 teamsolo systemd: Started PostgreSQL RDBMS.
lines 1-9/9 (END)
Complete a the php-pgsql driver install and CockroachDB connect request
- From a terminal window, use the
apt-get installcommand to install the php-pgsql driver.
Prove that the php-pgsql driver and PostgreSQL library are installed correctly.
Make a new PHP file and name it “index.php”.
Connect to the database and add the file with the code shown here.
// allow PHP to show any errors
// basic connection of a database
// Do a NAME_OF_DATABASE and NAME_OF_USER replacement appropriately
$dbconn = pg_connect("host=localhost port=26257 dbname=NAME_OF_DATABASE user=NAME_OF_USER")
or die('Could not connect: ' . pg_last_error());
echo ("CockroachDB connection: " . $dbconn);
Open a browser window and go to: localhost/php_cockroach/index.php
A successful result is like this one below. You should something similar.
This tutorial explained the steps for installing PHP driver in CockroachDB in Linux. The PHP-pgsql driver is the heartbeat that pumps communication signals to and from the database. Those “signals” are your commands and PHP requests to access the database and make complex queries in CockroachDB in your Linux OS. The php-pgsql driver is an integral part of the PostgreSQL object-relational database management system (ORDBMS). It’s your ally. Use it to harness the capabilities of fast inquiries and database management of your CockroachDB in Linux. Fully utilize the communication lifeline of the php-pgsql driver today.