Skip to content

Instantly share code, notes, and snippets.

@xx4159
Created November 28, 2016 06:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xx4159/546db9e63d38653d65918fbfdb5b825c to your computer and use it in GitHub Desktop.
Save xx4159/546db9e63d38653d65918fbfdb5b825c to your computer and use it in GitHub Desktop.
Migrate Mongodb to PostgreSQL

Migrate Mongodb to PostgreSQL

목적

  • 사용자의 로컬 테스트를 목적으로 쓰여진 내용입니다.
  • mac osx에서 진행했으며, ubuntu 나 centos같은 다른 리눅스 버전에서는 다를 수 있음!
  • This is FOR OSX
  • I hope we collaborate.

Prerequire

  • postgreSQL install.
  • mongodb가 replica setting.
  • mongodb schema 작성이 잘 되어있으면 좋음..
  • mosql install.

OSX(local)에 postgreSQL 설치하기

설치 경로

설정파일 위치

/usr/local/var/postgres/postgresql.conf

설치 방법 (brew)

  1. Install postgreSQL:

    brew install postgresql
    
  2. Create a user:

    • postgreSQL 서버를 띄울 프로세스와 db를 소유할 계정을 생성한다. (맥 users & groups 앱으로 해당 계정을 먼저 만들고 해야하나?)
    createuser -P postgres
    
  3. Create directory (named postgres)

    • postgres 디렉토리의 권한이 postgreSQL을 설치한 계정으로 되어있어서 지우고 다시 db 초기화가 필요하다.
    • brew install 을 postgres 계정으로 할 수 있다면 그렇게 하는게 더 깔끔하겠다. 나는 brew를 실행을 할 수있는 권한이나 /usr/local/var 에 디렉토리를 생성하는것도 su만 가능했다.
    rm -rf /usr/local/var/postgres
    sudo mkdir /usr/local/var/postgres
    sudo chmod 775 /usr/local/var/postgres
    sudo chown postgres /usr/local/var/postgres
    
  4. Initialize Database cluster:

    • postgres 계정으로 db 초기화(for a new installation and allows you to connect with the default postgres user.)
    su - postgres
    initdb /usr/local/var/postgres -E utf8
    
  5. Run Database(start the postgres server):

    # -D: database
    # -l: logfile
    pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
    # or run as background service `brew services start postgresql`

    etc.

    • postgres 라는 유저명으로 계정이나 db를 생성할 게 아니라면 다음 명령어로 새로운 db를 만들 수 있다. createdb `whoami`
    • postgreSQL cli 시작 psql
  6. Stop postgreSQL

    pg_ctl -D /usr/local/var/postgres stop -s -m fast
    

mosql 설치하기

gem install mosql

[issue] libpq

postgreSQL을 설치한 이후, mosql을 설치하던 중 Can't find the PostgreSQL client library (libpq) 라는 에러를 뱉어서 찾아보니 env ARCHFLAGS="-arch x86_64" gem install pg 를 실행하면 해결된다고 했더니 gem install mosql 이 순조롭게 진행되었다.

mosql 실행하기

# mosql [-c collections.yml] [--sql postgres://sql-server/sql-db] [--mongo mongodb://mongo-uri]
mosql -c pt-collections.yml --mongo mongodb://localhost:8000

mongodb replica 설정 후 start

# replica setting
# replica setting을 위해 몽고를 2개 이상 띄우지 않고 initiate 를 실행하는 것 만으로 mosql을 쓰는데는 지장이 없다.
mongo --path 8000
rs.initiate()
# start
mongod --port 8000 --dbpath "/data/db0" --replSet rs0

데이터 확인

postgreSQL Query 참고 URL

select * from tables limit 5;
select "nameEn" from tables;
# 세미콜론을 안쓰면 명령이 끝나지 않았다고 생각하나 봄.
# 나의 경우 데이터가 너무 많아서 안보였거나 한글이 깨지는거 같다.....?????

불안 요소

  • mosql 은 살려주는이 하나 없는 deprecated 상태..
  • 하지만 schema 작성도 잘 하고 csv로 export시키거나 하면 되겠지 뭐. 라는 안일한 생각을 갖고있음.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment