Services/Tool

[Supabase] Local 개발환경 셋팅

built 2023. 10. 6. 11:18

https://supabase.com/docs/guides/cli/local-development?access-method=postgres 

 

Local Development | Supabase Docs

To access the database from an edge function in your local Supabase setup, replace localhost with host.docker.internal.

supabase.com

 

기업협업을 하면서 Supabase를 경험하게 되었다.

위의 링크를 참고하여 순서대로 진행하면 된다. 하면 되는데...나는 3~4번을 삭제하고 재시작하면서 몇 번의 에러를 경험했다.

Supabase가 생소하고 링크도 이해되지 않는 분들을 위한 글이다.

초기 셋팅부터 순탄치 않아 구글링과 챗GPT와의 열띤 대화로 알아낸 모든 정보를 공유한다.

 


 

1 supabase 로그인

supabase login

NPM을 통해 설치한 경우 npx supabase login로 실행해야 한다.

 

 

 

 

2 새 폴더 생성 및 Git 저장소를 시작

mkdir new-project
cd new-project
git init

 

 

 

 

3 Supabase 서비스 시작

npx supabase init

로컬에서 프로젝트를 개발하기 위한 구성을 설정하기 위해 Supabase를 초기화한다.

 

npx supabase start

docker를 실행하고 supabase 서비스를 시작한다.

 

❗️여기서 나와 같은 오류가 뜬다면

Error response from daemon: driver failed programming external connectivity on endpoint supabase_db_
failed: port is already allocated

Try rerunning the command with --debug to troubleshoot the error.

 

포트 충돌로 인해 Docker 컨테이너가 외부 연결을 설정하지 못한 경우 발생했기 때문이다.

Docker를 중지 및 삭제하고 재시작해보면 에러를 해결할 수 있다.

 

docker stop $(docker ps -q)
docker rm $(docker ps -a -q)
docker-compose up -d

 

모든 Supabase 서비스가 실행되면 로컬 Supabase 프로젝트에서 사용할 URL과 키가 표시된다.

 

 

 

 

4 마이그레이션 파일 생성 및 SQL 추가

supabase migration new create_employees_table
create table
employees (
id bigint primary key generated always as identity,
name text,
email text,
created_at timestamptz default now()
);

생성된 파일에 SQL문을 붙여넣는다.

 

 

 

 

5 employees 테이블 생성 및 수정

supabase db reset

데이터베이스를 현재 마이그레이션으로 재설정하려면 재설정 명령어를 사용한다.

 

employees 테이블이 생성되었다!

 

 

supabase migration new add_department_to_employees_table

이제 대시보드에서 employees 테이블을 방문할 수 있다.
column을 추가하여 테이블을 수정하기 위해 새 마이그레이션 파일을 만든다.

 

 

alter table
if exists public.employees add department text default 'Hooli';

해당 파일에 SQL을 추가하여 새 column을 만든다.

 

 

npx supabase db diff --schema public

Supabase 데이터베이스 스키마의 변경 내용을 확인할 수 있다.

 

변동사항이 없을 경우 No schema changes found 라고 뜬다.

 

 

 

이렇게 로컬 개발 환경 셋팅 및 임시 테이블 마이그레이션 테스트 작업 끝~🐣