-
RESTful API를 이용하여 PostgreSQL 연계How to Dev 2021. 12. 14. 13:17
PostgREST PostgreSQL
- The PostgreSQL Global Development Group에서 개발하는 오픈 소스 ORDBMS. 1996년에 첫 출시되었다. 처음에는 BSD 라이선스였으나 언제부터인가 MIT 라이선스 비스무리한 독자적 라이선스[1]를 따르기 시작했다. 발음은 '포스트그레스큐엘'이라고 한다[나무위키 : https://namu.wiki/w/PostgreSQL]
REST API
- Rest 기반으로 컴퓨터 프로그램간의 상호 작용을 가능하게 하고 정보를 교환하게 구현한 어플리케이션
PostgreSQL과 REST API 구현
- 좀더 쉽게 REST API를 구현하기 위해 오픈소스를 참고
1. PostgREST(18000 star), https://github.com/PostgREST
2. pREST(3008 star), https://github.com/prestprest
pREST (PostgreSQL REST), low-code, simplify and accelerate development, ⚡ instant, realtime, high-performance on any Postgres application, existing or new - prest
github.com
PostgREST
PostgREST has 3 repositories available. Follow their code on GitHub.
github.com
준비 사항
- ubuntu
- docker
- postgresql docker imagePostgreSQL 및 PostgREST 설치
# PostgreSQL 설치 sudo docker run --name tutorial -p 5433:5432 \ -e POSTGRES_PASSWORD=mysecretpassword \ -d postgres # postgREST 다운로드 및 설치 # download from https://github.com/PostgREST/postgrest/releases/latest wget https://github.com/PostgREST/postgrest/releases/download/v9.0.0/postgrest-v9.0.0-linux-static-x64.tar.xz tar xJf postgrest-v9.0.0-linux-static-x64.tar.xz ./postgrest
PostgreSQL 이미지 다운로드 및 실행 PostgREST 설치 확인 API용 데이터 베이스 생성
-- psql (9.6.3) -- Type "help" for help. -- 명령 프롬프트 생성 -- postgres=# -- 스키마 생성 create schema api; -- todo 테이블 생성 create table api.todos ( id serial primary key, done boolean not null default false, task text not null, due timestamptz ); insert into api.todos (task) values ('finish tutorial 0'), ('pat self on back'); -- 익명 웹 요청 사용 역할 생성 create role web_anon nologin; grant usage on schema api to web_anon; grant select on api.todos to web_anon; -- postgres 대신 데이터베이스 연결을 위한 전용 역할 생성 create role authenticator noinherit login password 'mysecretpassword'; grant web_anon to authenticator; -- 종료 -- \q
테이블 생성 및 권한 부여 PostgREST 실행
# 환경 설정 파일 생성 touch tutorial.conf vi tutorial.conf # 파일 안에는 접속 정보 db-uri = "postgres://authenticator:mysecretpassword@localhost:5433/postgres" db-schema = "api" db-anon-role = "web_anon" # 실행 ./postgrest tutorial.conf
테스트
# get 요청 테스트 curl http://localhost:3000/todos # post 요청 테스트 # 권한 없음이 나와야 정상임 curl http://localhost:3000/todos -X POST \ -H "Content-Type: application/json" \ -d '{"task": "do bad thing"}'
REST API 테스트 'How to Dev' 카테고리의 다른 글
Python Selenium을 이용한 인스타그램 자동 내리기 (0) 2021.12.17 2021 Full-stack 개발자 (0) 2021.12.14 Istio란? (0) 2021.12.10 Visual Studio Code Javascript 라이브 로깅 (0) 2021.12.07 git pull error(강제 덮어쓰기) (0) 2021.09.16