[Linux] PostgreSQL 구성

슬터디·2023년 11월 30일
0

[YOU] 기술분석

목록 보기
8/24

설치 및 초기 구성

설치

  • dnf install postgresql-server -y

초기 구성

  • postgresql-setup initdb
    • initial 구성 수행

서비스 구동

  • systemctl enable postgresql; systemctl start postgresql
  • systemctl status postgresql
  • ss -pltn
    • 5432 포트를 Listen 중인 것 확인

User 및 DB 생성

  • su - postgres

    • PostgreSQL 설치 시 기본 계정 postgres가 생성됨
    • su: switch user
  • psql
    - postgreSQL 접속
    - [postgres=#]

  • CREATE ROLE dba;

  • CREATE DATABASE comento;

  • GRANT ALL PRIVILEGES ON DATABASE comento TO dba;

  • ALTER USER dba PASSWORD 'dba';

  • ALTER DATABASE comento OWNER TO dba;
    - 해당 계정에서 사용할 데이터베이스 연결

  • \l: db 목록 조회(list)

  • \q: psql 종료

DB 접속 설정 수정

  • 위의 상태에서는 외부에서 DB에 접속할 수가 없다. 따라서 다음 사항을 진행.

  • vim /var/lib/pgsql/data/postgresql.conf

    • Connections and Authentication 설정 내용을 담은 파일임.
    • listen_addresses = '*' # what IP address(es) to listen on;
      • 모든 대역의 ip 접속을 허용
  • vim /var/lib/pgsql/data/pg_hba.conf

    • PostgreSQL Client Authentication Config file
      (인증과 관련된 파일이라고 생각하면 된다.)
    • 기존 IPv4 host 관련 설정 주석 처리 후
    • host all all 0.0.0.0/0 trust 입력
  • su -: root 계정으로 돌아옴

    • [root@dmb01 ~]
    • 설정파일 수정 후에는 서비스를 재구동해야 설정이 적용되고, 이는 root 계정에서만 가능함
  • systemctl restart postgresql

로컬 접속 테스트

  • ss -pltn | grep post

    • grep: post에서 실행중인 프로세스를 찾는 것
    • LISTEN 0 128 0.0.0.0:5432 0.0.0.0:* users:(("postmaster",pid=1059,fd=3))
  • su - postgres

    • [postgres@dbm01 ~]
  • psql -h [dbmIP] -d comento -U dba -W

    • psql -U 유저명 -d DB명
    • -h host명: 서버가 돌아가는 호스트명
    • -W: 비밀번호를 입력하겠다
      -> comento=>

DB Restore

  • 웹에서 공유되고 있는 Sample Data로 comento를 복구시킨다.
  • DB 복원은 PostgreSQL의 가장 상위 계정인 postgres로 하고, 생성한 dba 계정으로 owner를 수정한다.
  • pg_restore -U postgres -d comento dvdrental.tar
    • pg_restore [옵션] [파일명]
    • -d: DB명
    • -U: 백업 시 접속할 사용자명

psql 접속 후,

  • \c comento

    • \c [DB name] [Connection User]
    • 해당 DB로 접속하겠다.
  • \dt

    • 접속한 DB Instance의 Table 목록 보기
  • ALTER TABLE actor OWNER TO dba;

    • 모든 테이블 owner를 dba로 수정하기

WAS에서 DB 접속 테스트

  • dnf install postgresql -y
  • psql -h [db ip] -d comento -U dba -W
profile
기억력이 맹구라 늘 기록해야해

0개의 댓글