This is the function you need if you are running into the infamous "must be superuser to COPY to or from a file" error from postgres.
(PHP 4 >= 4.0.3, PHP 5, PHP 7, PHP 8)
pg_put_line — Envía una string al servidor PostgreSQL
pg_put_line() envía una string (terminada
por null
) al servidor PostgreSQL. Esto es necesario en conjunción con
un comando COPY FROM
de PostgreSQL.
COPY
es una carga muy rápida de datos
soportada por PostgreSQL. Los datos se pasan sin ser analizados y
en una simple transacción.
Una alternativa en lugar de usar el comando bruto pg_put_line() es usar pg_copy_from(). Es una interfaz mucho más simple.
Nota:
Tenga en cuenta que la aplicación debe agregar explícitamente los dos caracteres "\." al final de la string para indicar al servidor que ha terminado de enviar datos, antes de llamar a pg_end_copy().
El uso de pg_put_line() hace que fallen la mayoría de los objetos de gran tamaño, incluyendo pg_lo_read() y pg_lo_tell(). Puede usar pg_copy_from() y pg_copy_to() en su lugar.
connection
An PgSql\Connection instance.
When connection
is unspecified, the default connection is used.
The default connection is the last connection made by pg_connect()
or pg_pconnect().
As of PHP 8.1.0, using the default connection is deprecated.
data
Una línea de texto para enviar directamente al servidor PostgreSQL. Un
carácter de finalización null
se agrega automáticamente.
Versión | Descripción |
---|---|
8.1.0 |
The connection parameter expects an PgSql\Connection
instance now; previously, a recurso was expected.
|
Ejemplo #1 Ejemplo con pg_put_line()
<?php
$conn = pg_pconnect("dbname=foo");
pg_query($conn, "create table bar (a int4, b char(16), d float8)");
pg_query($conn, "copy bar from stdin");
pg_put_line($conn, "3\tBonjour le monde\t4.5\n");
pg_put_line($conn, "4\tAurevoir le monde\t7.11\n");
pg_put_line($conn, "\\.\n");
pg_end_copy($conn);
?>
This is the function you need if you are running into the infamous "must be superuser to COPY to or from a file" error from postgres.
When using this function, don't get bit by using 'literal\tanotherliteral\n' issue by using single quotes vs. double quotes. "literal\tanotherliteral\n" is not the same. Many of the functions are impacted on how double quotes treats escape characters different than single quotes. I forget it all the time.