본문 바로가기
데이터베이스/Postgresql

[PostgreSql] 윈도우 db백업 및 리눅스 데이터 복원

by cbwstar 2021. 11. 8.
728x90
반응형

리눅스 접속

  1. su - postgres

 

Postgresql 터미널 접속

  1. psql -U postgres

 

/* 사용자 계정 생성 */

create user test password 'test2021' superuser;

 

/* 데이터 베이스 생성 */

create database test_db owner test;

 

/* 테이블 스페이스 저정될 폴더를 먼저 생성한다.*/

mkdir -p /postgre_db/test_dat

mkdir -p /postgre_db/test_idx

 

chown -R postgrs:postgres /postgre_db

 

 

/* 테이블 스페이스 생성 */

CREATE TABLESPACE test_tbl owner test LOCATION '/postgre_db/test_dat';

CREATE TABLESPACE test_idx owner test LOCATION '/postgre_db/test_idx';

 

/* 스키마 생성 */

\c test_db test

CREATE SCHEMA IF NOT EXISTS test AUTHORIZATION test;

 

/* 스키마 삭제 */

DROP SCHEMA test cascade;

 

/* 스키마 생성 확인 */

\dn

 

/* 윈도우 db 정보 dump */

/* db 전체 백업(윈도우) */

pg_dump.exe --file "C:\\postgre_dump\\test_full.dump" --host "localhost" --port "5432" --username "postgres" --password --verbose --format=c --section=pre-data --section=data --section=post-data --column-inserts --encoding "UTF8" "test_db"

 

 

/* db해당 스키마 백업(윈도우) */

pg_dump.exe --file "C:\\postgre_dump\\test.dump" --host "localhost" --port "5432" --username "postgres" --password --verbose --format=c --section=pre-data --section=data --section=post-data --column-inserts --encoding "UTF8" --schema "test" "test_db"

 

 

/* db전체복원(리눅스) */

/* db전체 복원하기 위해서는 사용자 계정과 테이블스페이스를 먼저 생성해 줘야 한다. */

pg_restore --host "192.168.1.42" --port "5432" --username "postgres" --password --dbname "test_db" --section=pre-data --section=data --section=post-data --verbose "/postgre_db/dump/test_full.dump"

 

 

 

/* 스키마 단위로 복원(리눅스) */

/* 스키마 단위로 복원은 사용자계정,테이블스페이스,스키마까지 생성하고 복원해야 한다. */

/* test */

pg_restore --host "192.168.1.42" --port "5432" --username "postgres" --password --dbname "test_db" --section=pre-data --section=data --section=post-data --verbose --schema "test" "/postgre_db/dump/test.dump"

 

/* 스키마 단위 복원(리눅스) 스키마 자동생성된다 */

pg_restore --host "192.168.1.42" --port "5432" --username "postgres" --password --dbname "test_db" --section=pre-data --section=data --section=post-data --verbose "/postgre_db/dump/test.dump"

 

 

/* 사용자 생성후 사용자로 로그인시 아래와 같은 오류 밣생시 */

[postgres@namenode ~]$ psql -U test

psql: 오류: 치명적오류:  사용자 "test" peer 인증을 실패했습니다.

[postgres@namenode ~]$ cat /var/lib/pgsql/13/data/pg_hba.conf

 

pg_hba.conf 파일을 열어서

로컬일 경우

local   all             all                                     peer

peer md5 변경하고 재시작 하여야 한다.

생성된 데이터 베이스 조회

\l

 

postgres=# \l

                                 데이터베이스 목록

   이름    |  소유주  | 인코딩 |   Collate   |    Ctype    |      액세스 권한     

-----------+----------+--------+-------------+-------------+-----------------------

 test      | naqsifh  | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 |

 postgres  | postgres | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 |

 template0 | postgres | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | =c/postgres          +

           |          |        |             |             | postgres=CTc/postgres

 template1 | postgres | UTF8   | ko_KR.UTF-8 | ko_KR.UTF-8 | =c/postgres          +

           |          |        |             |             | postgres=CTc/postgres

(4개 행)

 

 

생성된 데이터 베이스 접속

\c db 사용자

\c test_db test

 

postgres=# \c test_db test

test 사용자의 암호:

접속정보: 데이터베이스="test_db", 사용자="test".

test_db=#

 

/* 생성된 사용자 조회 */

select * from pg_shadow;

728x90
반응형

댓글



"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."

loading