Langsung ke konten utama

how to install postgresql and use it with php on termux

first of all you need to install it:
apt install postgresql

then specify data dir for it by typing:
initd -D YOUR_DATA_DIR

example YOUR_DATA_DIR is /data/data/com.termux/files/home/mydb

note that you don't need to make the dir first, you just need the dir name then initdb will create it automatically :)

in this example like this:
initdb -D /data/data/com.termux/files/home/mydb

The files belonging to this database system will be owned by user "u0_a171".
This user must also own the server process.

The database cluster will be initialized with locale "C.UTF-8".
The default text search configuration will be set to "english".

Data page checksums are disabled.

creating directory test ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... sysv
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... Asia/Jakarta
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.

Success. You can now start the database server using:

    pg_ctl -D /data/data/com.termux/files/home/mydb -l logfile start

as it says, you can now run the db server :)
by typing:
pg_ctl -D /data/data/com.termux/files/home/mydb -l logfile start

after that, you need to install modules for working with it in php.
by typing:
apt install php-pgsql apr-util

now in the php side;
if you have nor already installed the install it first by typing:
apt install php

the edit the php.ini file in /data/data/com.termux/files/usr/lib/

edit by adding this section:
extension=pdo_pgsql.so
extension=pgsql.so

and save it

then test it now by typing:
php -a


the type the following:
$servername = "127.0.0.1";
$username = "";
$password = "";

try {
    $conn = new PDO("pgsql:host=$servername;dbname=postgres", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "Connected successfully";
    }
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }

it will says Connected successfully if you did follow this post correctly :)

note you can change /data/data/com.termux/files/home/mydb to ~/mydb for short too

chers~

Komentar

Postingan populer dari blog ini

How to fix Apache httpd error: [:crit] [pid xxxx] (2)No such file or directory: AH00141: Could not initialize random number generator

How to fix Apache httpd error: [:crit] [pid xxxx] (2)No such file or directory: AH00141: Could not initialize random number generator. This error might be offen happen when using chroot  and no urandom in /dev folder of your chroot dir. Simply just mount the /dev folder to your chroot dir: mount -o bind /dev $chroot_dir/dev/ Then test it: httpd -v Server version: Apache/2.4.34 (Unix) Server built: Jul 19 2018 03:32:51