简体   繁体   中英

Import CSV to postgresql (Zend/PHP)

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.

\\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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM