slam / psql-php
PHP version of psql cli that comes with PostgreSQL
Fund package maintenance!
Slamdunk
paypal.me/filippotessarotto
Installs: 4 854
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- php: ~8.3.0 || ~8.4.0
- ext-pgsql: *
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.64.0
- phpstan/phpstan: ^1.12.4
- phpstan/phpstan-phpunit: ^1.4.0
- phpstan/phpstan-strict-rules: ^1.6.1
- phpunit/phpunit: ^11.3.6
This package is auto-updated.
Last update: 2024-11-21 03:30:07 UTC
README
PHP light version of psql
that comes with PostgreSQL.
Why
- You are inside a PHP only environment, like a PHP Docker image
- You need to import a large
pg_dump --inserts
dump - You don't have access to the native
psql
client
Performance
Speed is exactly the same of the original psql
binary thanks to streams usage.
Supported formats
When using pg_dump --inserts
it is highly recommended to also set --rows-per-insert=1000
to speed performances up.
Usage
The library provides two usages, the binary and the \SlamPsql\Psql
class.
From CLI
$ ./psql -h
Usage: psql [OPTIONS]
--host Connect to host
--port Port number
--username User for login
--password Password to use
--database Database to use
--connect_timeout Connect timeout to use
$ printf "CREATE DATABASE foobar;\nSELECT datname FROM pg_database;" | ./psql
foobar
$ ./psql --database foobar < foobar_huge_dump.sql
From PHP
$psql = new \SlamPsql\Psql('localhost', 5432, 'my_username', 'my_password', 'my_database'); $return = $psql->run(\STDIN, \STDOUT, \STDERR); exit((int) (true !== $return)); // With the connect_timeout argument $psql = new \SlamPsql\Psql('localhost', 5432, 'my_username', 'my_password', 'my_database', 5); $return = $psql->run(\STDIN, \STDOUT, \STDERR); exit((int) (true !== $return));
\SlamPsql\Psql::run
accepts any type of resource consumable by fgets/fwrite
functions.