ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • postgresql install and change data directory on Centos 7
    Tech/DS 2017. 6. 4. 06:08
    반응형

    현재 CentOS 7 에서 yum  으로 설치시 최신 버전이 9.2 인것 같다.

    현재 최신 베타 버전은 10 이다.

    안정화 버전인 9.6을 설치해보고 싶었고, 3억건의 데이터를 저장하기 위해 추가로 마운트 한 디스크로 데이터 디렉터리 위치를 변경하고 싶었다.


    현재 postgrsql 설치 가능한 버전 보기


    #yum list postgresql*


    설치 가능한 버전이 9.2 로 나온다.


    - rpm 으로 설치


    #rpm -Uvh https://yum.postgresql.org/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm


    재확인


    #yum list postgresql*

    설치 가능한 버전 목록 중에 postgresql96-server 가 보인다.

    설치


    #yum -y install postgresql96-server postgresql96-devel

    설정 파일 링크

    #ln -s /usr/pgsql-9.6/bin/pg_config /usr/bin/pg_config

    데이터베이스 초기화(mariadb 랑 다르게 처음 데이터베이스를 초기화 해주어야한다.)


    #/usr/pgsql-9.6/bin/postgresql96-setup initdb

    시작 해준다.


    #systemctl start postgresql-9.6

    재부팅시 자동으로 시작하도록 등록해준다.


    #systemctl enable postgresql-9.6


    현재 서버의 디스크가 루트는 20G 로 되어있고, 추가로 150G 로 늘린 상태이다.

    추가디스크를 마운트 시킨 디렉터리로 postgresql 의 data 디렉터리를 변경하도록 한다.


    디렉터리 변경 작업
    postgres 계정으로 로그인 한 후 psql 명령어를 실행 시킨다.(아래는 다이렉트로 접속)


    #sudo -u postgres psql
    postgres=# show data_directory;


    아래와 같이 현재 데이터 디렉터리 위치가 나온다

    => /var/lib/pgsql/9.6/data


    #mkdir /mnt/data/postgres
    #chmod 700 /mnt/data/postgres
    #chown -R postgres:postgres /mnt/data/postgres


    postgres 계정으로 로그인 한후 다시 데이터베이스를 초기화 해준다.


    #su - postgres


    $ cd /usr/pgsql-9.6/bin/


    변경된 디렉터리(해당 디렉터리에는 설정파일 백업파일 등이 포함되어있어야한다. )

    $ ./initdb -D /mnt/data/postgres


    bash profile 에서 데이터 디렉터리 경로를 변경하여 postgresql 에 적용하도록 한다.


    # vi /var/lib/pgsql/.bash_profile

    아래와 같이 변경해준다.(기존 PGDATA 는 주석 혹은 삭제 해준다.)
    =>PGDATA=/mnt/data/postgres

    시스템에 등록된 postgresql-9.6 서비스를 수정해준다.

    수정해주지 않으면 기존 설정을 그대로 유지 함으로써 원하는 디렉터리로 변경이 안된다.


    #vi /usr/lib/systemd/system/postgresql-9.6.service
    기존: Environment=PGDATA=/var/lib/pgsql/9.6/data/
    변경: Environment=PGDATA=/mnt/data/postgres/

    시스템 서비스를 다시 릴로드 해준다.


    # systemctl daemon-reload

    postgresql 을 시작 및 재부팅시 자동시작으로 등록해준다.


    # systemctl enable postgresql-9.6.service
    # systemctl start postgresql-9.6.service

    확인 방법

    아래와 같이 -D 뒤에 원하는 디렉터리로 되어있다면 성공한것이다.


    #ps aux
    => /usr/pgsql-9.6/bin/postmaster -D /mnt/data/postgres/

    안되면 시스템 데몬을 다시 릴로드 해본다
    #systemctl stop postgresql-9.6
    #systemctl daemon-reload

    외부접속 및 포트 오픈

    (기존에는 /var/lib/pgsql/9.6/... 이 경로였다. 하지만 이제는 변경된 디렉터리의 설정파일을 수정해야 한다.)

    #vi /mnt/data/postgres/postgresql.conf
    =>port=5432 주석 해제
    =>listen-address=‘*’
    #systemctl restart postgresql-9.6

    #vi /mnt/data/postgres/pg_hba.conf

    => host all 0.0.0.0/0 md5


    postgres 패스워드를 설정해준다.

    #sudo -u postgres psql
    #\password postgres
    Enter new passoword: xxxxx
    Enter it again:xxxxx
    #\q
    #systemctl restart postgresql-9.6


    http://www.sfentona.net/?p=2756 https://www.lesstif.com/pages/viewpage.action?pageId=31850584


    반응형

    'Tech > DS' 카테고리의 다른 글

    데이터베이스 인덱싱 생성 방법  (0) 2017.08.02
    Postgresql-9.6 Master Slave replication config  (0) 2017.06.06
    Postgresql daily auto partition  (0) 2017.06.02
    Postgresql session issue 해결방법  (0) 2017.06.02
    MariaDB Partioning  (0) 2017.06.02
Designed by Tistory.