이 문서는 PostgreSQL 설치에 대한 정보를 공유한다.
CentOS
CentOS 배포판 확인
다음 명령을 통해 postgresql 버전을 확인한다.
yum list | grep ^postgresql
설치해야할 postgresq 버전은 9.2 이상이므로 없을 경우 저장소 업데이트가 반드시 필요하다.
postgresql yum 저장소 업데이트
저장소 업데이트를 위해 linux 버전을 확인한다.
uname -a
하위 링크에서 버전에 맞는 rpm 주소를 복사한다.
rpm -Uvh https://download.postgresql.org/pub/repos/yum/9.3/redhat/rhel-6-x86_64/pgdg-centos93-9.3-3.noarch.rpm
postgresql 리스트를 다시 확인한다.
postgresql 설치
다음 명령을 통해 9.3 버전을 설치한다.
yum install postgresql93-server postgresql93 /usr/pgsql-9.3/bin/postgresql93-setup initdb
설치 패키지 확인
다음 명령을 통해 설치되었는지 확인한다.
rpm -qa|grep post
계정확인
하위 명령을 통해 postgres 계정이 있는지 확인 한다.
cat /etc/passwd
PostgreSQL 생성 경로
아래 파일에서 PGDATA 위치를 확인한다.
- /var/lib/pgsql/.bash_profile
기본적으로 아래 위치로 설정되어 있다.
- /var/lib/pgsql/9.3/data
만약 데이터 저장 위치를 변경하고 싶다면, vi를 통해 PGDATA의 위치를 변경한다.
PostgreSQL 초기화
initdb는 postgreSQL을 초기화해준다. 아래 명령을 통해 초기화를 수행한다.
/etc/init.d/postgresql-9.3 initdb
PostgreSQL 구동
다음 명령을 통해 데이터베이스를 실행합니다.
/etc/init.d/postgresql-9.3 start or systemctl start postgresql-9.3
PostgreSQL 정지
다음 명령을 통해 데이터베이스를 실행합니다.
/etc/init.d/postgresql-9.3 stop or systemctl stop postgresql-9.3
Postgres 패스워드 변경
다음 명령을 수행하여 postgres 패스워드를 변경합니다.
- sudo passwd postgres
PSQL 접속
다음 명령을 통해 psql로 접속하고 설치 버전을 확인합니다.
su - postgres psql select version();
PSQL 명령문
계정 리스트
- \du
계정 생성
- CREATE ROLE username;
계정 삭제
- DROP ROLE username;
계정 권한
- CREATE ROLE username WITH LOGIN;
데이터베이스에 권한 부여
- GRANT ALL ON DATABASE databasename TO username
데이터베이스 리스트
- \l
- \list
- SELECT datname FROM pg_database;
데이터베이스 생성
- CREATE DATABASE dbname;
- 예) CREATE DATABASE jiradb WITH ENCODING 'UNICODE' LC_COLLATE 'C' LC_CTYPE 'C' TEMPLATE template0;
데이터베이스 삭제
- DROP DATABASE dbname;
데이터베이스 소유자 변경
- ALTER DATABASE dbname OWNER TO username;
PostgreSQL 삭제
yum으로 설치했다면 yum으로 삭제합니다.
yum remove *postgresql*
사용자 삭제는 다음 명령을 이용합니다.
- userdel postgres
사용자 폴더 삭제는 다음 명령을 이용합니다.
- rm -rf /home/pgsql
PostgreSQL 서버 외부에서 접속할 경우 설정
Open postgresql.conf file and add the following line to the end: listen_addresses = '*' Add the following line to the end of /var/lib/pgsql/data/pg_hba.conf file: host all all 203.0.113.2/32 md5