# PostgreSQL에서 R 사용하기 (PL/R extension 설치)

Jung Junkyo·2025년 5월 5일
0

📌 시스템 환경

  • OS: Red Hat Enterprise Linux 8.10
  • DB: PostgreSQL 16
  • R: 4.5.0-3.el8 (EPEL)
  • PL/R 버전: 8.4.7

1. R 설치

💡 사전 준비

R을 설치하기 위해선 여러 dependency를 직접 다운로드해 설치가 필요하다.
(유료 구독을 쓰는 사람들은 논외)

📦 수동 설치한 패키지 목록

# R 설치를 위한 dependency
openblas-0.3.15-6.el8.x86_64.rpm

# openblas-0.3.15-6.el8.x86_64.rpm 설치를 위한 dependency list
openblas-devel-0.3.15-6.el8.x86_64.rpm
openblas-openmp-0.3.15-6.el8.x86_64.rpm
openblas-openmp64-0.3.15-6.el8.x86_64.rpm
openblas-openmp64_-0.3.15-6.el8.x86_64.rpm
openblas-serial64-0.3.15-6.el8.x86_64.rpm
openblas-serial64_-0.3.15-6.el8.x86_64.rpm
openblas-threads64-0.3.15-6.el8.x86_64.rpm
openblas-threads64_-0.3.15-6.el8.x86_64.rpm

위 패키지들은 일반 repo에서는 구할 수 없어 수동으로 rpm을 다운받아 설치 진행


2. PL/R 설치

📥 PL/R .rpm 다운로드

wget https://download.postgresql.org/pub/repos/yum/16/redhat/rhel-8-x86_64/plr_16-8.4.7-1PGDG.rhel8.x86_64.rpm

📂 서버에 업로드 및 설치

# 업로드 확인
ls | grep plr
# 출력 예시:
plr_16-8.4.7-1PGDG.rhel8.x86_64.rpm

# 설치
sudo dnf install ./plr_16-8.4.7-1PGDG.rhel8.x86_64.rpm

3. PostgreSQL에서 PL/R 확장 사용

🔍 확장 가능한지 확인

SELECT * FROM pg_available_extensions WHERE name = 'plr';
 name | default_version | installed_version |                            comment
------+-----------------+-------------------+----------------------------------------------------------------
 plr  | 8.4.7           |                   | load R interpreter and execute R script from within a database

✅ 확장 생성 및 버전 확인

CREATE EXTENSION plr;
SELECT plr_version();
 plr_version
-------------
 8.4

4. 마무리

✅ 간단한 테스트 함수

CREATE OR REPLACE FUNCTION r_now() RETURNS text AS $$
  sprintf("R time is now: %s", Sys.time())
$$ LANGUAGE plr

SELECT r_now();
                   r_now
-------------------------------------------
 R time is now: 2025-05-05 06:00:52.607928
(1 row)

PostgreSQL 내부에서 직접 R 코드를 실행


profile
DB specialist를 꿈꾸는 초짜

0개의 댓글