Skip to content

Commit

Permalink
Merge pull request #17 from INgandle/feature/postgis-setup
Browse files Browse the repository at this point in the history
🔨 [Chore] PostGIS 설정
  • Loading branch information
kimdoyeonn authored Aug 30, 2024
2 parents 94d55ff + 2b9857a commit 19ad57d
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 22 deletions.
5 changes: 3 additions & 2 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# node server
NODE_ENV=production
NODE_ENV=development

# postgres 설정

Expand All @@ -8,7 +8,8 @@ POSTGRES_PORT=5432


# postgres host
POSTGRES_HOST=
# dev - localhost, production - matjum-db 라고 써주세요.
POSTGRES_HOST=loclahost

# postgres database 이름
POSTGRES_DB=
Expand Down
24 changes: 24 additions & 0 deletions Dockerfiles/db/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM postgres:16.4-bullseye

# PostGIS 주 버전 설정
ENV POSTGIS_MAJOR=3
ENV POSTGIS_VERSION=3.4.2+dfsg-1.pgdg110+1

# 필요한 패키지 설치 및 PostGIS 설치
# 인증서 오류 발생할 경우 ca-certificates 패키지 설치
RUN apt-get update \
&& apt-cache showpkg postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR \
&& apt-get install -y --no-install-recommends \
postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR=$POSTGIS_VERSION \
postgresql-$PG_MAJOR-postgis-$POSTGIS_MAJOR-scripts \
&& rm -rf /var/lib/apt/lists/*

RUN mkdir -p /docker-entrypoint-initdb.d
# PostGIS 초기화 스크립트 추가
COPY ./initdb-postgis.sh /docker-entrypoint-initdb.d/10_postgis.sh
# PostGIS 업데이트 스크립트 추가
COPY ./update-postgis.sh /usr/local/bin

RUN chmod +x /docker-entrypoint-initdb.d/10_postgis.sh

ENV POSTGRES_DB=gis
7 changes: 7 additions & 0 deletions Dockerfiles/db/initdb-postgis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
set -e

echo "Loading PostGIS extensions into $POSTGRES_DB"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE EXTENSION IF NOT EXISTS postgis;
EOSQL
16 changes: 16 additions & 0 deletions Dockerfiles/db/update-postgis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/sh
set -e

# Perform all actions as $POSTGRES_USER
export PGUSER="$POSTGRES_USER"

# PostGIS 버전 설정
POSTGIS_VERSION="${POSTGIS_VERSION%%+*}"

# $POSTGRES_DB에 PostGIS 설치, 업데이트
echo "Installing PostGIS extension to $POSTGRES_DB"
psql --dbname="$POSTGRES_DB" -c "
-- Install PostGIS (includes raster)
CREATE EXTENSION IF NOT EXISTS postgis VERSION '$POSTGIS_VERSION';
ALTER EXTENSION postgis UPDATE TO '$POSTGIS_VERSION';
"
File renamed without changes.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,10 @@

## 컨테이너 종료

- `npm run docker:dev:down` 혹은 `npm run docker:prod:down` 명령어로 컨테이너를 종료할 수 있습니다.
- `npm run docker:dev:stop` 혹은 `npm run docker:prod:stop` 명령어로 컨테이너를 종료할 수 있습니다.
- `docker stop` 이나 `docker compose down -f <파일명>` 명령어로도 종료할 수 있습니다.

## docker volume 삭제해야 할 때 (DB 테이블 변경 등)

- database 다 날아가니 주의해서 사용해주세요.
- `docker volume rm matjum_db_data` 명령어로 삭제합니다.
4 changes: 3 additions & 1 deletion docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
services:
db:
container_name: matjum_db
image: postgres:16.4-alpine
build:
context: ./Dockerfiles/db
restart: always
env_file:
- .env
volumes:
- ./init.sql:/docker-entrypoint-initdb.d/init.sql
- db_data_dev:/var/lib/postgresql/data
ports:
- '${POSTGRES_PORT}:5432' # Host:Container 내부 포트
Expand Down
19 changes: 14 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
services:
db:
container_name: matjum_db
image: postgres:16.4-alpine
container_name: matjum-db
build:
context: ./Dockerfiles/db
restart: always
env_file:
- .env
Expand All @@ -14,20 +15,28 @@ services:
interval: 10s
timeout: 5s
retries: 5
networks:
- matjum-network
server:
container_name: matjum_server
container_name: matjum-server
depends_on:
db:
condition: service_healthy
build:
context: .
dockerfile: Dockerfile
context: ./Dockerfiles/server
env_file:
- .env
ports:
- '3000:3000'
init: true
restart: unless-stopped # 예기치 않은 종료 시 재시작
networks:
- matjum-network

volumes:
db_data:

networks:
matjum-network:
name: matjum-network
driver: bridge
13 changes: 7 additions & 6 deletions src/entities/member.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Column, Entity, OneToMany } from 'typeorm';
import { Column, Entity, OneToMany, Point } from 'typeorm';

import { BaseModel } from './base-model.entity';
import { Review } from './review.entity';
Expand All @@ -15,11 +15,12 @@ export class Member extends BaseModel {
@Column({ type: 'varchar', length: 255, select: false })
password: string;

@Column({ type: 'real', nullable: true })
lon: number;

@Column({ type: 'real', nullable: true })
lat: number;
/**
* 4326 - WGS 84 좌표계, 위도와 경도를 도(degrees) 단위로 표현함, 지구의 곡률을 고려하여 정확하게 거리를 계산하기 위해 필요함
* 기본값은 0, 평면 좌표계
* */
@Column({ type: 'geometry', nullable: true, srid: 4326 })
location: Point;

@Column({ default: false })
isRecommendationEnabled: boolean;
Expand Down
14 changes: 7 additions & 7 deletions src/entities/restaurant.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Column, Entity, OneToMany, Unique } from 'typeorm';
import { Column, Entity, OneToMany, Point, Unique } from 'typeorm';

import { BaseModel } from './base-model.entity';
import { Review } from './review.entity';
Expand All @@ -19,12 +19,12 @@ export class Restaurant extends BaseModel {
@Column({ type: 'varchar', length: 512, nullable: true })
address: string;

// real: 실수형 데이터 타입, 4byte
@Column({ type: 'real', nullable: true })
lon: number;

@Column({ type: 'real', nullable: true })
lat: number;
/**
* 4326 - WGS 84 좌표계, 위도와 경도를 도(degrees) 단위로 표현함, 지구의 곡률을 고려하여 정확하게 거리를 계산하기 위해 필요함
* 기본값은 0, 평면 좌표계
* */
@Column({ type: 'geometry', nullable: true, srid: 4326 })
location: Point;

@Column({ type: 'real', default: 0 }) // review 가 없을 경우 0으로 설정
rating: number;
Expand Down

0 comments on commit 19ad57d

Please sign in to comment.