개발일지

[postgreSQL] 우분투(Ubuntu 20.04)에서 dbf파일 import 하기

송채채 2023. 9. 14. 17:18

서버환경

우분투 20.04

postgreSQL 12

postgreSQL 설치 (원격)

sudo apt-get update
sudo apt-get install postgresql

관련 설정 경로)

/etc/postgresql/12/main

 

shp 파일을 다루려면 postGIS과 shp2pgsql을 사용함

 

참고)

- 한글이 들어간 데이터를 업로드할 예정이라 우분투의 locale을 ko_KR.UTF-8로 변경함

 

DB 생성

 

postgres(superuser)로 접속

sudo -u postgres psql

DB 생성 시 인코딩 설정함

CREATE DATABASE address WITH ENCODING 'UTF-8' LC_COLLATE='ko_KR.UTF-8' LC_CTYPE='ko_KR.UTF-8' TEMPLATE=template0;

참고자료)

문자 집합 지원 https://www.postgresql.kr/docs/10/multibyte.html

[PostgreSQL] 데이터베이스 & 스키마 & 테이블 생성법

 

 

postGIS 설치

공식홈페이지에서 안내해준 ubuntu 기준 설치방법

https://trac.osgeo.org/postgis/wiki/UsersWikiPostGIS3UbuntuPGSQLApt

 

UsersWikiPostGIS3UbuntuPGSQLApt – PostGIS

The ​PostgreSQL Apt Repository now hosts installs of PostGIS, pgRouting, pgpointcloud in addition to PostgreSQL. The following describes how to install PostgreSQL 14, PostGIS 3.2, pgRouting 3.4 on an Ubuntu/Debian system. It is assumed to also work on Li

trac.osgeo.org

apt install postgresql-12-postgis-3
apt install postgis
# 하는 김에 pgrouting도 설치함
apt install postgresql-12-pgrouting
apt install osm2pgrouting

 

shp2pgsql로 데이터 삽입

참고-shp2pgsql의 경로

/usr/bin/shp2pgsql

 

데이터 넣을 때는 경로 상관없이 아래 명령어 입력

shp2pgsql -c -D -n -s 5179 -W "cp949" /home/data/test.dbf | psql -U postgres -d address
더보기
  • `-c`: Creates a new table and populates it, this is the  default if you do not specify any options
    데이터를 새로운 테이블에 적재하도록 하는 옵션입니다. 새로운 테이블을 생성하고 데이터를 그 테이블에 저장
  • `-D`: Use postgresql dump format (defaults to SQL insert statements).
    PostgreSQL 덤프 형식 사용. 대용량 데이터세트 적재 시 사용
  • `-n`: Only import DBF file.
    공간 도형없이 속성정보만 필요한 경우, 이 옵션을 사용
  • - `-s 5179`: <srid> Set the SRID field. Defaults to 0.
    SRID (공간 참조 ID)를 설정하는 옵션
  • `-W "cp949"`: <encoding> Specify the character encoding of Shape's attribute column. (default: "UTF-8")
    Shapefile의 문자 인코딩을 설정하는 옵션입니다.  한글이 포함된 데이터인 경우 "cp949"로 설정
  •  `/home/data/test.dbf`: DBF 파일의 경로
  • `|`: 파이프 (`|`) 기호는 명령의 출력을 다음 명령으로 전달하는 파이프 라인을 생성
  • `psql -U postgres -d address`: PostgreSQL 데이터베이스 `address`에 로그인하는 `psql` 명령어
    `-U` 옵션은 사용자 이름을 지정하고 `-d` 옵션은 데이터베이스 이름을 지정함. 

 

결과창

파일명 기준으로 테이블 생성함

참고)

- 이미 존재한 테이블이 있다면 옵션 -c 삭제 후 -a로 추가할 수 있음

- 특정 테이블을 지정할 경우, 경로 뒤에 테이블명 작성

 

아래 블로그에 옵션과 설명이 상세히 있으니 같이 보는걸 추천

shp2pgsql - Postgresql에 Shapefile 업로드하기

shp2pgsql로 postgreSQL에 shp파일 올리기 (윈도우)

반응형