人生襍多

postgresql 세팅 본문

프로그래밍

postgresql 세팅

이혁진 2022. 11. 9. 14:21

1. install

- postgresql는 설치는 root계정으로 apt로 설치하면 됨 
- postgres 사용자가 그냥 생기네 
- 설치위치 & 실재 data는 '/var/lib/postgresql/12/main'  <-- postgres 사용자 
- 실행파일 위치는 '/usr/lib/postgresql/12' <-- root 사용자 
- 설정파일들은 '/etc/postgresql/12/main/'  <-- postgres 사용자 
- 시스템로그는 '/var/log/postgresql/' 여기에  <-- root 사용자  

- 나중에 이거 한번 testing 해보자 https://atl.kr/dokuwiki/doku.php/postgresql_%EB%B0%B1%EC%97%85_%EB%B0%8F_%EB%B3%B5%EA%B5%AC_-_%EB%AA%85%EB%A0%B9%EC%96%B4_%EC%82%AC%EC%9A%A9 


2. 원격 접속을 위한 환경 설정 방법 

- 변경한 후 서버를 재시작
- #service postgresql-9.4 restart

$sudo vi /etc/postgresql/12/main/postgresql.conf
* listen_addresses = '*'  # 모든 IP에 대한 remote 접속 허용합니다.
* port = 5432             # 원하는 port번호를 설정합니다.
* listen_addresses = '*'                  # what IP address(es) to listen on;
                                          # comma-separated list of addresses;
                                          # defaults to 'localhost'; use '*' for all
 
$sudo vim /etc/postgresql/9.5/main/pg_hba.conf
* host all all 0.0.0.0/0 md5
* host all all 192.1.1.0/24 md5


3. 초기설치후 DB사용할 계정 추가 

- 계정이라고 적긴했는데, Postgresql에서는 role이라고 한다. 
- postgres=# 은 superuser를 의미하고, postgres=> 는 superuser 가 아님을 의미한다.

$sudo -i -u postgres
$psql

postgres=# 
CREATE ROLE 사용할명칭 WITH LOGIN PASSWORD '사용할명칭암호';

# DB의 계정 및 role 권한 정보 확인
\du 

ALTER ROLE eklee CREATEDB;

# 개발기라 이렇게 
alter user 사용할명칭 with superuser;
ALTER ROLE 사용할명칭 NOSUPERUSER


$psql -d postgres -U 사용할명칭
postgres=> 
CREATE DATABASE 사용할디비명;

4. psql 간단명령어 


postgres=> 
\q : psql 종료
\c : 새로운 db 접속
\dt : 테이블 리스트
\du : Role과 유저 리스트
\list : DB 리스트

- 관리자 비밀번호 설정

$ sudo -i -u postgres
$ psql

postgres=# \password postgres
Enter new password:[암호]
Enter it again:[암호 재입력]
postgres=# \q

- PostgreSQL 백업 방법
$ pg_dump mgdb > postgres_mgdb_bak.sql


최대 파일 크기가 제한된 운영체제도 있어, 이 크기를 초과하는 경우에 문제가 될 수 있으며 이런경우 압축과 같은 기능을 이용하여 해결할 수 있습니다.
# 백업 시
$ pg_dump DB_NAME | gzip > postgres_DB_NAME_bak.gz
 
# 복원 시
$ gunzip -c postgres_DB_NAME_bak.gz | psql DB_NAME

SQL 덤프 복원방법
참고로 SQL 덤프백업에서는 데이터베이스와 사용자 생성 쿼리가 없기 때문에,

복원할 데이터베이스와 사용자는 사전에 만들어 주어야 합니다.
$ psql DB_NAME < postgres_DB_NAME_bak.sql

 

참조

https://justhodl.tistory.com/53
https://dbza.tistory.com/entry/PostgreSQL-%EC%84%A4%EC%B9%98-%EB%B0%8F-%EC%84%A4%EC%A0%95

Comments