0

I want to load test data into a table in my postgresql database.

I am doing this inside my unit test class.

$this->Application_Model_UserMapper->getDbTable()->getAdapter()->query("COPY users FROM '" . APPLICATION_PATH . "/../tests/dbtestdata/users.csv' CSV HEADER NULL AS E'\N'");

and I get an error message that says I must be a super user to copy to and from a file. Does that mean I need to log in as postgres user? I have read that that could cause security issues, so I am not keen on doing that.

Other option seems to be \COPY, but the following doesnt work..

$this->Application_Model_UserMapper->getDbTable()->getAdapter()->query("

  \COPY users FROM '" . APPLICATION_PATH . "/../tests/dbtestdata/users.csv' 
  CSV HEADER NULL AS E'\N'

");

I get a syntax error.

1 Answer 1

1

\copy is used by psql, a PostgreSQL client. The SQL statement COPY is done by the user Postgres, your code needs this user or a different solution.

I really like pg_put_line, then you can read the file in PHP and stream it to PostgreSQL.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the tip! I have almost made it work using the code here : php.net/manual/en/function.pg-put-line.php but for some reason, COPY is not recognizing my column and row delimiters (\t and \n respectively). I solved the column delimiter issue by changing it to '|', but I am unable to provide more than 1 row of data. Would you have any suggestions?
pastebin.com/srwFLdut It is not recognizing the \n so i cannot insert more than 1 line.
I fixed it by replacing the \n with an actual enter key. Any idea why this would happen? Is it because I am using a mac?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.