https://github.com/slamdunk/psql-php
PHP version of psql cli that comes with PostgreSQL
https://github.com/slamdunk/psql-php
php postgresql psql
Last synced: 8 months ago
JSON representation
PHP version of psql cli that comes with PostgreSQL
- Host: GitHub
- URL: https://github.com/slamdunk/psql-php
- Owner: Slamdunk
- License: mit
- Created: 2023-09-04T07:28:46.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-05-01T18:19:10.000Z (about 2 years ago)
- Last Synced: 2024-05-01T21:28:14.069Z (about 2 years ago)
- Topics: php, postgresql, psql
- Language: PHP
- Homepage:
- Size: 79.1 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `psql` in PHP
[](https://packagist.org/packages/slam/psql-php)
[](https://packagist.org/packages/slam/psql-php)
[](https://github.com/Slamdunk/psql-php/actions)
[](https://codecov.io/gh/Slamdunk/psql-php?branch=master)
PHP light version of `psql` that comes with PostgreSQL.
## Why
1. You are inside a PHP only environment, like a PHP Docker image
1. You need to import a large `pg_dump --inserts` dump
1. 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
| Input type | Example | Supported? |
|--------------------------------------------------------|---------------------------------------------|:------------------:|
| `pg_dump` output (with `COPY` commands) | *as is* | :x: |
| `pg_dump --inserts` output | *as is* | :heavy_check_mark: |
| Single query on single line | `SELECT NOW();` | :heavy_check_mark: |
| Single query on multiple lines | `SELECT`
`NOW();` | :heavy_check_mark: |
| Multiple queries on separated single or multiple lines | `SELECT NOW();`
`SELECT`
`NOW();` | :heavy_check_mark: |
| Multiple queries on single line | `SELECT NOW();SELECT NOW();` | :x: |
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
```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.