Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7528229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:19:14+00:00 2026-05-30T04:19:14+00:00

COPY table_name ( field1, field2, field3) FROM STDIN CSV; 1,2,"q w" 3,4,"a s" 5,6,d

  • 0
COPY table_name ( field1, field2, field3) FROM STDIN CSV;
1,2,"q w"
3,4,"a s"
5,6,d
\.

How to execute this query by PDO ?

Update:

Problem is PDO driver executes this query as statement.
For example, if you paste it into pgAdmin, it throws an error.
I need execute it in psql:

C:\Users\User>psql -e -h localhost -U postgres db_name
psql (9.1.2)
db_name=# COPY table_name ( field1, field2, field3) FROM STDIN CSV;
COPY table_name ( field1, field2, field3) FROM STDIN CSV;
Enter data to be copied followed by a newline.
End with a backslash and a period on a line by itself.
>> 1,2,"q w"
>> 3,4,"a s"
>> 5,6,d
>> \.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:19:15+00:00Added an answer on May 30, 2026 at 4:19 am

    Thanks to this book

    Note that the function presented here effectively by-passes security restrictions, which are there for a reason. Your function should check the file path and table provided against strict white list conditions. This example is also open to SQL injection as it does not quote its input correctly.

    Create function which execute COPY command

    CREATE OR REPLACE FUNCTION copy_from_csv_ignoring_security(table_name text, table_fieds text, file_path text, oids boolean DEFAULT false, header boolean DEFAULT false, delimeter text DEFAULT ','::text, "null" text DEFAULT ''::text, quote text DEFAULT '"'::text, escape text DEFAULT '"'::text, force_not_null text DEFAULT ''::text)
      RETURNS void AS
    $BODY$
    
    declare statement text;
    begin
    
    statement := 'COPY ' || table_name || ' (' || table_fieds || ') ' || 'FROM ''' || file_path || ''' WITH ';
    IF oids THEN
     statement := statement || 'OIDS ';
    end if;
    statement := statement || 'DELIMITER ''' || delimeter || ''' ';
    statement := statement || 'NULL ''' || "null" || ''' CSV ';
    IF header THEN
     statement := statement || 'HEADER ';
    end if;
    statement := statement || 'QUOTE ''' || "quote" || ''' ';
    statement := statement || 'ESCAPE ''' || "escape" || ''' ';
    IF force_not_null <> '' THEN
    statement := statement || 'FORCE NOT NULL ''' || force_not_null || ''' ';
    end if;
    execute statement;
    end;
    $BODY$
      LANGUAGE plpgsql VOLATILE SECURITY DEFINER
      COST 100;
    

    Give rights on function

    revoke all on function copy_from_csv_ignoring_security(text, text, text, boolean, boolean, text, text, text, text, text) from public;
    grant execute on function copy_from_csv_ignoring_security(text, text, text, boolean, boolean, text, text, text, text, text) to db_user;
    

    Execute from PHP

    $dbh->exec('SELECT copy_from_csv_ignoring_security(...)');
    

    ===== If version >= 9.1.7 trick above doesn’t works. =====

    Solution:

    create file .pgpass (avoid password prompt) in home directory of user which run this script.

    #.pgpass contents (chmod 600 - requred)    
    host:port:db_name:user_name:password
    

    create php function, which executes meta-command

    function executeMetaCommand($dbUser, $dbName, $dbPort, $command)
    {    
        $command = sprintf(
            "psql -U %s -p %s -d %s -f - <<EOT\n%s\nEOT\n",
            $dbUser, $dbPort, $dbName, $command
        );
        $streams = array(
            array('pipe', 'r'),// stdin
            array('pipe', 'w'),// stdout
            array('pipe', 'w') // stderr
        );
        $process = proc_open($command, $streams, $pipes);
        if (!is_resource($process)) {
            throw new Exception("Cannot open process:\n$command");
        } else {
            list(, $stdout, $stderr) = $pipes;
            $error = stream_get_contents($stderr);
            fclose($stderr);
            if (strlen($error) > 0) {
                throw new Exception("Process error:\n$error");
            } else {
                $output = stream_get_contents($stdout);
                fclose($stdout);
                $returnCode = proc_close($process);
                if ($returnCode === -1) {
                    throw new Exception("Process was completed incorrectly:\n$output");
                } else {
                    return array(
                        $returnCode,
                        $output
                    );
                }
            }
        }
    }
    
    //usage:
    $command = sprintf("\\copy table(field1, field2) FROM '%s' WITH CSV", $filePath);
    executeMetaCommand('postgres', 'test_db', '5432', $command);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the SQL command to copy a table from one database to another
Using VB 6 and Access 2003 I want to copy a table from one
Basically, I'm trying to selectively copy a table from one database to another. I
I have two identical tables and need to copy rows from table to another.
I am trying to copy data from my MYSQL table to SQL Server using
I have to copy a bunch of data from one database table into another.
I am attempting to insert a copy of a row from one table into
I'm using INSERT INTO to copy rows of data from one table to another:
I need to copy data from one table to another. The tables do not
Edit 4: the problem came from using the string name of the column instead

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.