Search code examples
postgresqlcopypgadmin

pgAdmin3: Using query results to COPY files


I want to copy several hundred text files into my postgres database using pgAdmin3 (on a Mac OSX 10.9.5). I've created a list of all the filenames and their paths, which I have copied into a table named 'filenames' in my database. I'm trying to set up a WHILE loop to open each file:

Create NewTable (NewData varchar(255)); 
SET @i=1;
    WHILE @i<10
    BEGIN
        SET @V1=(select path_and_filename from filenames where row_number=@i);
        PRINT @V1;
        COPY NewTable FROM @V1;
        SET @i=@i+1;
    END

Here's the result:

[PGSCRIPT ] ("/Volumes/WINDOWS COM/Weather ISD-lite/2013/010100-99999-2013")
[WARNING  ] COPY NewTable FROM ("/Volumes/WINDOWS COM/Weather ISD-lite/2013/010100-99999-2013")
            ERROR:  syntax error at or near "("
            LINE 1: COPY NewTable FROM ("/Volumes/WINDOWS COM/Weather ISD-lite/2...

I've also tried the following:

DECLARE @R1 {@A};
SET @i=1;
WHILE @i<10
BEGIN
    SET @R1=(select path_and_filename from filenames where row_number=@i);
    SET @V1=@R1[0][0];
    PRINT @V1;

    COPY NewTable FROM @V1;

    SET @i=@i+1;
END

Here's the result:

[PGSCRIPT ] /Volumes/WINDOWS COM/Weather ISD-lite/2013/010100-99999-2013
[WARNING  ] COPY NewTable FROM /Volumes/WINDOWS COM/Weather ISD-lite/2013/010100-99999-2013
            ERROR:  syntax error at or near "/"
            LINE 1: COPY NewTable FROM /Volumes/WINDOWS COM/Weather ISD-lite/201...
                                       ^

Solution

  • It looks from the error like the path name is not quoted, and so therefore produces a syntax error.

    Try changing this:

    COPY NewTable FROM @V1;

    to this:

    COPY NewTable FROM '@V1';