74

10 列の CSV ファイルがあります。4 列の PostgreSQL テーブルを作成した後、10 列の一部をテーブルにコピーしたいと考えています。

私のCSVテーブルの列は次のようになります:

x1 x2 x3 x4 x5 x6 x7 x8 x9 x10

私のPostgreSQLテーブルの列は次のようになります:

x2 x5 x7 x10
4

8 に答える 8

88

アドホックなタスクの場合

入力ファイル内のすべての列を含む一時テーブルを作成します

create temporary table t (x1 integer, ... , x10 text)

ファイルからそれにコピーします。

copy t (x1, ... , x10)
from '/path/to/my_file'
with (format csv)

次に、一時テーブルから決定的なテーブルに挿入します。

insert into my_table (x2, x5, x7, x10)
select x2, x5, x7, x10
from t

そしてドロップします:

drop table t

頻繁に行う作業であれば

file_fdw拡張子を使用します。スーパーユーザーとして:

create extension file_fdw;

create server my_csv foreign data wrapper file_fdw;

create foreign table my_csv (
    x1 integer,
    x2 text,
    x3 text
) server my_csv
options (filename '/tmp/my_csv.csv', format 'csv' )
;

テーブルを読み取るユーザーに、テーブルに対する選択権限を付与します。

grant select on table my_csv to the_read_user;

次に、必要に応じて、csv ファイルをテーブルのように直接読み取ります。

insert into my_table (x2)
select x2
from my_csv
where x1 = 2
于 2012-09-27T11:04:22.110 に答える
5

James Brown の提案をさらに進めて、すべて 1 行で実行できます。

$ awk -F ',' '{print $2","$5","$7","$10}' file | psql -d db -c "\copy MyTable from STDIN csv header"
于 2017-03-08T13:41:13.903 に答える
1

To load data from spreadsheet (Excel or OpenOffice Calc) into postgreSQL:

Save the spreadsheet page as a CSV file. Prefered method is to open the spreadsheet on OpenOffice Calc and do the saving. On “Export to text file” window choose Character Set as Unicode (UTF8), Field Delimiter: “,” and Text Delimiter “ “ “. Message will be displayed saying only active sheet is saved. Note: This file has to be saved on a folder but not on desktop and have to save in UTF8 format (postgreSQL by dafault is step up for UTF8 encoding). If saved on desktop, postgreSQL will give “access denied” message and won't upload.

In PostgreSQL, create an empty table with same number of column as the spreadsheet.

Note: On each column, column-name has to be same, data type has to be same. Also, keep in mind the length of data where character varying with enough field.

Then on postgreSQL, on SQL window, put the code:

copy "ABC"."def" from E'C:\\tmp\\blabla.csv' delimiters ',' CSV HEADER;

NOTE: Here C:\\tmp is the folder where CSV-file “blabla” is saved. “ABC”.”def” is the table created on postgreSQL where "ABC" is schema and"def" is the actual table. Then do “execute query” by pressing the green button on top. “CSV HEADER” is needed when CSV table has heading at the start of every column.

If everythig is ok, no error message will be displayed and table data from CSV file will be loaded into the postgreSQL table. But if there is an error message do as following:

If error message is saying that the data is too long for a specific column, then increase the column size. This happens mostly on character and character varying column. Then run the “execute query” command again.

If error message is saying that the data type doesn't match to a particular column, then change the data type on postgreSQL table-column to match the one in CSV table.

In your case, after creating CSV file, delete the unwanted columns and match the columns in postgre table.

于 2012-10-04T06:12:05.033 に答える