PostgreSQL 데이터 복사 - PostgreSQL deiteo bogsa

반응형

PostgreSQL에서 

새로운 테이블을 만들때 다른 테이블을 복사하여 생성하는 방법에 대해 알아보자.

바로바로 예제를 통해 접근해보자.

(예제에 나오는 othertable 에 복사할려는 테이블 명을 적어주면 된다.)


▷예제1) 다른 테이블의 구조만 복사하기

CREATE TABLE mine (LIKE othertable);

▷예제2) 다른 테이블의 구조와 데이터 복사하기

CREATE TABLE it AS SELECT * FROM othertable

▷예제3) 다른 테이블의 구조와 테이블의 모든 정보 복사하기(ft. index)

create table trecord (like othertable including all);

-- 추가로 데이터도 넣어주기
insert into trecord (select * from othertable);

반응형

저작자표시

1. 데이터 복사.

CREATE TABLE 새로운테이블명 AS SELECT * FROM 복사할테이블명;

2. 데이터, 인덱스, constraint 등 정보도 복사.

create table 새로운테이블명 (like 복사할테이블명 including all);

insert into 새로운테이블명 ( select * from 복사할테이블명);

3. 다른 테이블의 필드 선택 복사.

insert into 새로운테이블명 (복사할필드명)

select 복사할필드명 from 복사할테이블명 where 조건문;

  • 다른 테이블 구조 & 데이터 복사하기 

    다른 테이블 구조와 데이터 복사하기 

    CREATE TABLE newtable AS SELECT * FROM oldtable;

    다른 테이블 구조만 복사하기

    CREATE TABLE newtable ( LIKE oldtable ); 

    데이터 & 인덱스 &  constraint 등의 정보 다 같이 복사하기 

    아래 두개의 sql 문을 차례로 실행한다. 복사 속도는 위의 방식이 더 빠르지만 인덱스 정보가 같이 복사됨.

    create table newtable (like "oldtable" including all);

    insert into newtable ( select * from "oldtable");

    다른 테이블의 일부 필드만 복사하기 

    insert into items_ver(item_id, item_group, name)

    select * from items where item_id=2;

    insert into items_ver (item_id, name, item_group)

    select item_id, name, item_group from items where item_id=2;

    TAG

1. pgAdmin 에서 복사하고자 하는 테이블을 우클릭 한 후 'Backup'을 클릭한다.

PostgreSQL 데이터 복사 - PostgreSQL deiteo bogsa

2. 'General'탭에서 Filename 은 원하는 대로 작성, Format은 'Plain'으로 변경한다.

PostgreSQL 데이터 복사 - PostgreSQL deiteo bogsa

3. 'Dump Options'탭 에서  'Only data' 혹은 'Only Schema' 을 선택해서 진행하여야 한다.

   - Only data 는 데이터만 복사, Only Schema 는 테이블 구조를 복사한다.

   - 그러기 때문에, 테이블+데이터를 모두 복사할거라면 한번씩 선택하여 총 2번 진행하여야 한다.

PostgreSQL 데이터 복사 - PostgreSQL deiteo bogsa

4. 오른쪽 아래 'Backup'버튼을 누르면 .backup 파일이 나온다.

6. 이제 생성된 .backup 파일을 NotePad로 열어서 확인한 후, 복사+붙여넣기로 테이블 생성/데이터 삽입을 하면 완료된다. 

 - 왼쪽 스크린샷은 스키마 복사(Only Schema) 결과, 오른쪽 스크린샷은 데이터 복사(Only data) 결과

PostgreSQL 데이터 복사 - PostgreSQL deiteo bogsa

stackoverflow.com/questions/3195125/copy-a-table-from-one-database-to-another-in-postgres

Copy a table from one database to another in Postgres

I am trying to copy an entire table from one database to another in Postgres. Any suggestions?

stackoverflow.com

PostgreSQL 데이터 복사 - PostgreSQL deiteo bogsa

'PostgreSql' 카테고리의 다른 글

[PostgreSql] pgAdmin4 를 이용하여 Table 복사 하는법 (같은DB/다른DB), 'Backup' 사용  (0) 2021.05.11
[Postgresql] CSV File 을 Table 에 import 하는법 (pgAdmin4)  (0) 2021.03.23