2026년 2월 1일 일요일

Greenplum에서 RESTful API 사용하기

 Greenplum에서 RESTful API 사용하기


Greenplum 7.6+에서 PostgREST 13을 지원


## 1. RESTAPI를 위한 DB 계정 및 테스트 테이블


-- 1) API 전용 스키마 생성

CREATE SCHEMA api;


-- 2) 샘플 테이블 생성

CREATE TABLE api.todos (

id serial PRIMARY KEY,

done boolean DEFAULT false,

task text NOT NULL,

due timestamptz

) DISTRIBUTED BY (id); -- Greenplum 특유의 DISTRIBUTED 절


-- 3) 데이터 삽입

INSERT INTO api.todos (task)

VALUES ('finish tutorial 0'), ('pat self on back');


SELECT * FROM api.todos;

id|done |task |due|

--+-----+-----------------+---+

2|false|pat self on back | |

1|false|finish tutorial 0| |

-- 4) 익명 사용자용 역할(Role) 생성

-- 실제 로그인 권한은 없으며, PostgREST가 이 권한으로 전환하여 쿼리합니다.

CREATE ROLE web_anon NOLOGIN;

GRANT USAGE ON SCHEMA api TO web_anon;

GRANT SELECT ON api.todos TO web_anon;


-- 5) 인증용 관리자 역할 생성 (PostgREST가 DB에 실제 접속할 때 사용)

CREATE ROLE authenticator NOINHERIT LOGIN PASSWORD 'changeme';

GRANT web_anon TO authenticator;


## 2. PostgREST 13 설치 및 구동

-- 1) Greenplum PostgREST 13.0.4-1 for RHEL 9 다운로드

-- https://support.broadcom.com/

-- 다운로드 파일 : postgrest-13.0.4-1.el9.x86_64.rpm


-- 2) PostgREST 설치

# yum install postgrest-13.0.4-1.el9.x86_64.rpm

# which postgrest

/usr/bin/postgrest

# ls -la /usr/bin/postgrest

-rwxr-xr-x 1 root root 20148784 Jul 11 2025 /usr/bin/postgrest


-- 3) PostgREST 설정

-- postgrest.conf 설정

$ cat << EOF > $COORDINATOR_DATA_DIRECTORY/postgrest.conf # Prepare CONFIGURATION

# postgrest.conf


# Greenplum Coordinator 노드의 접속 정보

db-uri = "postgres://authenticator:changeme@172.16.65.31:5432/gpkrtpch"


# API로 노출할 스키마

db-schemas = "api"


# 인증되지 않은 요청이 사용할 역할

db-anon-role = "web_anon"


# 서버 포트 설정

server-port = 3000


# (선택) PostgREST 13 신규 기능: 스키마 캐시 최적화

# Greenplum은 스키마가 방대할 수 있으므로 필요한 경우 설정

db-pool = 10

EOF


-- postgrest.conf 설정 확인

$ cat $COORDINATOR_DATA_DIRECTORY/postgrest.conf


-- 4) PostgREST 구동

$ postgrest $COORDINATOR_DATA_DIRECTORY/postgrest.conf



## 3. RESTAPI 테스트

-- 1) 테이블 조회

[gpadmin@r9g7s1 ~]$ curl http://172.16.65.31:3000/todos

[{"id":2,"done":false,"task":"pat self on back","due":null},

{"id":1,"done":false,"task":"finish tutorial 0","due":null}]

[gpadmin@r9g7s1 ~]$


-- 2) 테이블 조회 및 Json 문서로 출력

[gpadmin@r9g7s1 ~]$ curl http://172.16.65.31:3000/todos | jq

% Total % Received % Xferd Average Speed Time Time Time Current

Dload Upload Total Spent Left Speed

100 123 100 123 0 0 7687 0 --:--:-- --:--:-- --:--:-- 8200

[

{

"id": 2,

"done": false,

"task": "pat self on back",

"due": null

},

{

"id": 1,

"done": false,

"task": "finish tutorial 0",

"due": null

}

]


## 4. RESTAPI Features

-- 1) Filtering

$ curl "http://172.16.65.31:3000/todos?id=eq.1"

=# SELECT * FROM api.todos WHERE id = 1;


$ curl "http://172.16.65.31:3000/todos?id=eq.1&done=eq.false"

=# SELECT * FROM api.todos WHERE id = 1 AND done = false;


$ curl "http://172.16.65.31:3000/todos?task=like.*tutorial*"

=# SELECT * FROM api.todos WHERE task LIKE '%tutorial%';


-- 2) sorting

$ curl "http://172.16.65.31:3000/todos?order=id.asc"

=# SELECT * FROM api.todos ORDER id;


$ curl "http://172.16.65.31:3000/todos?order=id.desc"

=# SELECT * FROM api.todos ORDER id desc;


-- 3) pagination

$ curl "http://172.16.65.31:3000/todos?limit=15&offset=30"

=# SELECT * FROM api.todos LIMIT 15 OFFSET 30;

Greenplum에서 RESTful API 사용하기

  Greenplum에서 RESTful API 사용하기 Greenplum 7.6 +에서 PostgREST 13 을 지원 ## 1. RESTAPI를 위한 DB 계정 및 테스트 테이블 -- 1) API 전용 스키마 생성 CREATE SCHEMA ...