새로 설치하는 중이라면 initdb 를 하기 전에 systemd 유닛의 PGDATA 를 먼저 고치는 편이 낫다. 이미 돌고 있는 것을 옮기는 절차는 아래와 같다.
아래는 PostgreSQL 12 기준이다. 다른 계열은 경로의 12 와 서비스 이름의 숫자만 바꾼다.
show data_directory;
data_directory
------------------------
/var/lib/pgsql/12/data
systemctl stop postgresql-12
소유자·권한·심볼릭 링크를 보존해야 한다. cp -r 은 이것들을 잃으므로 쓰지 않는다.
mkdir -p /postgresql
cp -a /var/lib/pgsql/12/data /postgresql/
또는
rsync -aHAX /var/lib/pgsql/12/data/ /postgresql/data/
옮긴 뒤 소유자를 확인한다. postgres 가 아니면 서버가 기동하지 않는다.
ls -ld /postgresql/data
chown -R postgres:postgres /postgresql/data
chmod 700 /postgresql/data
배포판 유닛 파일을 직접 고치면 패키지 업데이트 때 덮인다. 드롭인을 쓰는 편이 안전하다.
systemctl edit postgresql-12
[Service]
Environment=PGDATA=/postgresql/data
유닛 파일을 직접 고치려면 다음 줄을 바꾼다.
vi /usr/lib/systemd/system/postgresql-12.service
# Location of database directory
#Environment=PGDATA=/var/lib/pgsql/12/data/
Environment=PGDATA=/postgresql/data/
postmaster.opts는 고치지 않는다. 서버가 기동할 때 마지막으로 사용한 명령줄 옵션을 스스로 기록하는 파일이라 편집해도 다음 기동 때 덮인다.[1]
systemctl daemon-reload
systemctl start postgresql-12
systemctl status postgresql-12
show data_directory;
data_directory
------------------
/postgresql/data
SELinux 를 쓰는 환경에서 기본 경로 밖으로 옮기면 기동이 막힌다. 컨텍스트를 붙여야 한다.
semanage fcontext -a -t postgresql_db_t "/postgresql/data(/.*)?"
restorecon -Rv /postgresql/data
검증: 미확인 — 위 SELinux 절차는 공식 PostgreSQL 문서가 아니라 일반적인 방식이다. 적용 전에 직접 확인할 것.
https://www.postgresql.org/docs/current/storage-file-layout.html — 2026-09-16 확인. "A file recording the command-line options the server was last started with" ↩︎