Postgres Pg_connect PHP

Started by Tory M Bluealmost 17 years ago9 messagesgeneral
Jump to latest
#1Tory M Blue
tmblue@gmail.com

Good day,

I'm having one heck of a time here. I'm looking for a clean solution
to issue a COPY statement from a remote system. (because I really
don't want to play the ssh, sudo spiel).

It's evident that pg_connect doesn't like the \copy command (it's a no
go), however it is okay with the COPY command, however the COPY
command requires superuser and I really don't want to open up the DB
from a remote system with Superuser access.

Soooo, what am I missing, misunderstanding ? There has to be a way,
seems from reading this has been brought up but I have not found a
solution.

I must be able to do this from a client system, I must be able to lock
down the db user to limited access and I can take care of permissions
on the client end, but postgres seems to be slightly rigid in regards
to the COPY command and the fact that one can't really have a jailed
superuser. I also understand that it's a pg_connect limitation to not
accept the \copy variant which would solve me issues.

Help me Rhonda, help help me..

Thanks
Tory

#2Joshua D. Drake
jd@commandprompt.com
In reply to: Tory M Blue (#1)
Re: Postgres Pg_connect PHP

I must be able to do this from a client system, I must be able to lock
down the db user to limited access and I can take care of permissions
on the client end, but postgres seems to be slightly rigid in regards
to the COPY command and the fact that one can't really have a jailed
superuser. I also understand that it's a pg_connect limitation to not
accept the \copy variant which would solve me issues.

Help me Rhonda, help help me..

http://www.php.net/manual/en/function.pg-copy-from.php

?

Joshua D. Drake

Thanks
Tory

--
PostgreSQL - XMPP: jdrake@jabber.postgresql.org
Consulting, Development, Support, Training
503-667-4564 - http://www.commandprompt.com/
The PostgreSQL Company, serving since 1997

In reply to: Joshua D. Drake (#2)
Re: Postgres Pg_connect PHP

however the COPY
command requires superuser and I really don't want to open up the DB
from a remote system with Superuser access.

COPY FROM STDIN does not need superuser privileges.

#4Tory M Blue
tmblue@gmail.com
In reply to: Vyacheslav Kalinin (#3)
Re: Postgres Pg_connect PHP

On Tue, Jun 9, 2009 at 11:31 AM, Vyacheslav Kalinin<vka@mgcp.com> wrote:

however the COPY
command requires superuser and I really don't want to open up the DB
from a remote system with Superuser access.

COPY FROM STDIN does not need superuser privileges.

Thanks guys, the problem with copy from or to is that it creates an
array and thus puts a load of stuff in memory, it's possible the file
will get huge and I can't take that memory hit.

I'll look again and see if I missed something

Thanks
Tory

In reply to: Tory M Blue (#4)
Re: Postgres Pg_connect PHP

On Tue, Jun 9, 2009 at 10:35 PM, Tory M Blue <tmblue@gmail.com> wrote:

Thanks guys, the problem with copy from or to is that it creates an
array and thus puts a load of stuff in memory, it's possible the file
will get huge and I can't take that memory hit.

I'll look again and see if I missed something

Thanks
Tory

It's not hard to do some streaming with copy (untested):

$conn = pg_pconnect("dbname=foo");
$fd = fopen('file.dat', 'r');
while (!feof($fd)) {
pg_put_line($conn, fgets($fd));
}
fclose($fd);
pg_put_line($conn, "\\.\n");
pg_end_copy($conn);

http://ru.php.net/manual/en/function.pg-put-line.php

In reply to: Vyacheslav Kalinin (#5)
Re: Postgres Pg_connect PHP

Forgot about COPY command in my previous reply:

$conn = pg_pconnect("dbname=foo");
$fd = fopen('file.dat', 'r');
pg_query($conn, "copy bar from stdin");
while (!feof($fd)) {
pg_put_line($conn, fgets($fd));
}
fclose($fd);
pg_put_line($conn, "\\.\n");
pg_end_copy($conn);

#7Dimitri Fontaine
dimitri@2ndQuadrant.fr
In reply to: Vyacheslav Kalinin (#6)
Re: Postgres Pg_connect PHP

Vyacheslav Kalinin <vka@mgcp.com> writes:

$conn = pg_pconnect("dbname=foo");

Please reconsider and use plain pg_connect().
--
dim

#8A B
gentosaker@gmail.com
In reply to: Dimitri Fontaine (#7)
Re: Postgres Pg_connect PHP

Vyacheslav Kalinin <vka@mgcp.com> writes:

 $conn = pg_pconnect("dbname=foo");

Please reconsider and use plain pg_connect().

Would you like to elaborate on that? Why connect and not pconnect?

In reply to: Dimitri Fontaine (#7)
Re: Postgres Pg_connect PHP

On Thu, Jun 11, 2009 at 4:36 PM, Dimitri Fontaine <dfontaine@hi-media.com>wrote:

Vyacheslav Kalinin <vka@mgcp.com> writes:

$conn = pg_pconnect("dbname=foo");

Please reconsider and use plain pg_connect().
--
dim

Uh, I just copied/pasted that part from somewhere in PHP manual, personally
I tend to use plain pg_connect and pgbouncer for pooling when needed.For
those curious why pconnect is not the best option for connection pooling -
discussion on this topic rises in this list from time to time, check this
out, for instance:
http://archives.postgresql.org/pgsql-general/2007-08/msg00660.php