살인!
postgresql
postgresql - postgresql 엑셀 CSV 화일로 테이블 export/import 하기
이 름 : 바다아이
|
조회수 : 8977
짧은 주소 : https://www.bada-ie.com/su/?41591823371
테이블 데이터를 텍스트화일로 export 해보겠습니다.
[postgres@olmaster:~/oradba/csv]$ alias scott
alias scott='psql -d scottdb -U scott -W '
[postgres@olmaster:~/oradba/csv]$ scott
Password for user scott:
Null display is "NULL".
Timing is on.
Pager is always used.
psql (9.2.1)
Type "help" for help.
scott@[local]:5432 scottdb#SQL> \copy emp to '~/oradba/csv/emp1.csv';
Time: 1.570 ms
scott@[local]:5432 scottdb#SQL> \! cat ~/oradba/csv/emp1.csv
7369 SMITH CLERK 7902 1980-12-17 800 \N 20
7499 ALLEN SALESMAN 7698 1981-02-20 1600 300 30
7521 WARD SALESMAN 7698 1981-02-22 1250 500 30
7566 JONES MANAGER 7839 1981-04-02 2975 \N 20
7654 MARTIN SALESMAN 7698 1981-09-28 1250 1400 30
7698 BLAKE MANAGER 7839 1981-05-01 2850 \N 30
7782 CLARK MANAGER 7839 1981-06-09 2450 \N 10
7788 SCOTT ANALYST 7566 1982-12-09 3000 \N 20
7839 KING PRESIDENT \N 1981-11-17 5000 \N 10
7844 TURNER SALESMAN 7698 1981-09-08 1500 0 30
7876 ADAMS CLERK 7788 1983-01-12 1100 \N 20
7900 JAMES CLERK 7698 1981-12-03 950 \N 30
7902 FORD ANALYST 7566 1981-12-03 3000 \N 20
7934 MILLER CLERK 7782 1982-01-23 1300 \N 10
scott@[local]:5432 scottdb#SQL> \copy emp(empno,ename ) to '~/oradba/csv/emp2.csv';
Time: 0.480 ms
scott@[local]:5432 scottdb#SQL> \! cat ~/oradba/csv/emp2.csv
scott@[local]:5432 scottdb#SQL> \copy emp(empno,ename) to '~/oradba/csv/emp3.csv' with delimiter ',' csv header
Time: 0.357 ms
scott@[local]:5432 scottdb#SQL> \! cat ~/oradba/csv/emp3.csv
scott@[local]:5432 scottdb#SQL>
이제 새 테이블을 생성후에, emp3.csv 화일을 import 해보겠습니다.
[postgres@olmaster:~/oradba/csv]$ cat emp3.csv
[postgres@olmaster:~/oradba/csv]$ scott
scott@[local]:5432 scottdb#SQL> create table customer(no integer, name varchar(32));
scott@[local]:5432 scottdb#SQL> \copy customer(no,name) from '~/oradba/csv/emp3.csv' with delimiter ',' csv header
scott@[local]:5432 scottdb#SQL> select * from customer;
scott@[local]:5432 scottdb#SQL>
오라클에 비하면, PostgreSQL 의 이런 기능은 너무 좋네요.
-----------------2013.07.12 추가 ------------------
필드값 내에 "," 가 들어가 있다면 다음과 같이 하면 됩니다.
[enterprisscottdb@dpcpdb03 oradba]$ cat a.csv
1,2,"super,man" ,"spider","man"
[enterprisscottdb@dpcpdb03 oradba]$ psql
psql (9.2.4.8)
Type "help" for help.
scottdb=# create table ccc(a varchar(10), b varchar(10), c varchar(10), d varchar(10), e varchar(10));
CREATE TABLE
scottdb=# \copy ccc(a,b,c,d,e) from '~/oradba/a.csv' with csv quote '"'
scottdb=# select * from ccc;
a | b | c | d | e
---+---+-----------+--------+-----
1 | 2 | super,man | spider | man
(1 row)
scottdb=#
cf. 컬럼변환이 필요하면
\copy ( SELECT CUST_NO ,TO_CHAR(REG_DTTM,'YYYY-MM-DD HH24:MI:SS') FROM ZZZ) to '/archive/dw.dat' with delimiter '|'
cf. export
copy (select * from from sql_trace
where snap_dt >= '$DATE2'
and snap_dt < '$DATE3'
order by snap_dt)
to '/Postgres/9.2/oradba/sql_move.csv' CSV QUOTE '"'
출처 : http://www.postgresdba.com/bbs/board.php?bo_table=B13&wr_id=4
|
|