PostgreSQL - \copy command

天大地大妈咪最大 提交于 2021-01-27 11:25:45

问题


I tried the above code. I manage to Compile. However, when I run, it give me error:

Exception in thread "main" org.postgresql.util.PSQLException: ERROR: syntax error at or near "\"
Position: 1

It shows that the query that I run is wrong:

String query ="\\COPY tmp from 'E:\\load.csv' delimiter ',';";

The System.out.println for the query is: query string: \COPY tmp from 'E:\load.csv' delimiter ',';

I run the query : \COPY tmp from 'E:\load.csv' delimiter ','; in PostgresSQL client, it works.

What happened?

Class.forName (driver);
conn = DriverManager.getConnection(host+dbname,user,password);
stmt = (java.sql.Statement) conn.createStatement();

//
PreparedStatement prepareUpdater = null;

conn.setAutoCommit(false);

String query ="\\COPY tmp from 'E:\\load.csv' delimiter ',';";

System.out.print("query string: "+query);

System.out.println("Query:"+query);
prepareUpdater = conn.prepareStatement(query);
prepareUpdater.executeUpdate();
prepareUpdater.close();

回答1:


PostgreSQL COPY statement exists in two mutation - server side COPY and psql side \copy. Both statements has same syntax and very similar behave. There are significant difference - \copy is working with client side file system. COPY is working with server side file system. psql \copy should not be called as server side SQL command. It is used directly from psql or from some bash scripts.

Server side COPY is used for massive export/import operation. When it working with file system, then it can be used only by user with super user rights. Unprivileged users has to use stdin, stdout target, but application have to support COPY API.

psql supports it - so you can use it for copy some table from one table to other table:

psql -c "COPY mytab TO stdout" db1 | psql -c "COPY targettab FROM stdin" db2

For Java Environment, you have to use some support like CopyManager. See how to copy a data from file to PostgreSQL using JDBC?

Import by COPY statement can be significantly faster than by INSERT statements - but in dependency on some additional overhead - The difference will be large on plain table without lot of indexes and without slow triggers. If you have lot of indexes on table or slower triggers, then the speedup from COPY will be marginal.




回答2:


Thank you for your feedback.

I managed to load *.csv using CSVReader.

I download CSVReader package and include it in my codes.

Its working fine.

 CsvReader products = new CsvReader("/tmp/ip2location/IP-COUNTRY-REGION-CITY-LATITUDE-LONGITUDE-ISP-DOMAIN-MOBILE-USAGETYPE.CSV");

             products.readHeaders();

             while (products.readRecord())
             {
                String ip_from = products.get("ip_from"); //int
                String ip_to = products.get("ip_to"); //int


                PreparedStatement prepareStat = null;

                String sqlIinsert = "insert into ip2location_tmp(ip_from, ip_to )"
        + " VALUES ("+ip_from+","+ip_to+");";

                System.out.println("sqlInsert:"+sqlIinsert);
                prepareStat = conn.prepareStatement(sqlIinsert); 
                prepareStat.executeUpdate();  

               }
        products.close();
        }enter code here



回答3:


You use COPY instead of /COPY

String query ="COPY tmp from 'E://load.csv' delimiter ','";

It will work




回答4:


The COPY SQL command reads a file from the local file system and the server process must have access to it.

The \copy command is a psql instruction and is only available within that environment. You cannot use that in a SQL query.

Since Java has a good CSV reader class, you could simply read the file in your code and then use individual INSERT commands to load the data in the database. This is effectively what \copy does when working in psql.



来源:https://stackoverflow.com/questions/30472170/postgresql-copy-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!