본문 바로가기
🗄️ DB_이야기/# 🐘 PostgreSQL

🐘[PostgreSQL] 환경변수 및 DB 기동/정지 정리

by gwon_s 2025. 7. 28.

🔍 들어가며

PostgreSQL 설치는 마쳤지만, 막상 DB를 기동(start) 하려니 명령어가 생각나지 않거나 기동이 안 되는 경우가 있습니다.
특히 환경변수가 설정되지 않았거나 디렉터리 지정이 안 됐을 때 오류가 자주 발생합니다.

 

또한, PostgreSQL을 사용할 때 매번 긴 경로를 입력하기 번거롭다면 환경변수를 설정해두면 pg_ctl 명령어를 훨씬 간편하게 사용할 수 있습니다.

 

이번 글에서는 PostgreSQL 환경변수 설정, 그리고 DB 기동 및 정지 명령어를 정리해 보겠습니다.


🔨환경변수 설정

[postgres@gwon-main ~]$ vi .bash_profile

...(생략)...
export PG_HOME=/postgres/app/postgres/pgsql17
export PGDATA=$PG_HOME/data
export PGLIB=$PG_HOME/lib
export PATH=$PG_HOME/bin:$PATH
export PGPORT=5432
export LD_LIBRARY_PATH=/postgres/app/postgres/lib

 

 

  • 환경변수 설명
환경변수 설명
export PG_HOME=/postgres/app/postgres/pgsql17 PGHOME은 PostgreSQL의 기본 홈 디렉토리를 설정
export PGDATA=$PG_HOME/data PGDATA는 PostgreSQL 데이터 파일이 저장된 디렉토리
export PGLIB=$PG_HOME/lib PGLIB는 PostgreSQL 관련 명령어 도구(initdb, pg_ctl, psql 등)가 내부적으로 라이브러리 경로를 참조할 때 사용하는 변수

PostgreSQL 명령줄 유틸리티나 스크립트에서 PGLIB를 참조할 수 있습니다.
export PATH=$PG_HOME/bin:$PATH PATH 환경변수는 시스템이 실행할 수 있는 명령어의 경로를 정의
export PGPORT=5432 PGPORT는 PostgreSQL 서버가 통신하는 포트번호
export LD_LIBRARY_PATH=/postgres/app/postgres/lib LD_LIBRARY_PATH는 리눅스에서 프로그램이 실행될 때 필요한 공유 라이브러리(.so 파일) 들을 찾는 경로를 지정합니다.


PostgreSQL 클라이언트나 유틸리티를 실행할 때 해당 라이브러리를 찾지 못하면 이 설정이 필요함

 

 

  • 수정된 환경변수 적용
[postgres@gwon-main ~]$ source .bash_profile

 

 


 

📞 Alias 설정

[postgres@gwon-main ~]$ vi .bashrc

...(생략)...
# DB OPEN
alias pgstart='/postgres/app/postgres/pgsql17/bin/pg_ctl -D /postgres/app/postgres/pgsql17/data start'

# DB STATUS
alias pgstatus='/postgres/app/postgres/pgsql17/bin/pg_ctl -D /postgres/app/postgres/pgsql17/data status'

# DB STOP
alias pgstop='/postgres/app/postgres/pgsql17/bin/pg_ctl -D /postgres/app/postgres/pgsql17/data stop'

 

 

  • .bashrc 수정된 내용 적용
[postgres@gwon-main ~]$ source .bashrc

 

 


 

🔧 PostgreSQL 기동 명령어

  • alias 적용 전 기동 명령어
[postgres@gwon-main ~]$ $PG_HOME/bin/pg_ctl start -D $PGDATA -l {logfile_name}

 

💡 pg_ctl 명령은 root가 아닌 PostgreSQL 실행 계정 (보통 postgres) 로 실행해야 합니다.

 

  • alias 적용 후 기동 명령어
[postgres@gwon-main ~]$ pgstart

💡 기동 시 로그파일 경로는 기본적으로 postgresql.conf의 설정을 읽어 자동으로 결정됩니다.

    즉, 별도로 -l logfile 옵션을 주지 않아도, postgresql.conf 안에 정의된 로그 관련 항목을 기준으로 로그가 남습니다.

 

 


 

📌 명령어 설명

 

항목 설명
pg_ctl PostgreSQL 제어 명령어 (start, stop, restart 등 가능)
-D 데이터 디렉토리 위치 (postgresql.conf, pg_hba.conf 포함)
-l 로그파일 위치 (서버 기동 시 발생하는 로그 기록)