Database

PostgreSQL 설치하기 (MacOS 기준)

kmmguumnn 2019. 12. 17. 05:10

MacOS에서 PostgreSQL을 설치하고 설정하는 방법을 정리해본다.
terminal에서 Homebrew를 통해 진행한다.

brew update
brew install postgres

Database를 생성하기 위해 아래의 명령어를 입력한다.

initdb /usr/local/var/postgres

아마도 아래와 같은 에러가 발생할 것이다.

initdb: error: directory "/usr/local/var/postgres" exists but is not empty
If you want to create a new database system, either remove or empty
the directory "/usr/local/var/postgres" or run initdb
with an argument other than "/usr/local/var/postgres".

이는 physical database를 생성하고자 하는 해당 폴더가 이미 존재하기 때문이다. 아래의 명령어로 모두 삭제한다.

rm -r /usr/local/var/postgres

다시 시도해보면, 정상적으로 initdb 명령어가 수행될 것이다.

initdb /usr/local/var/postgres

다음 명령어를 수동으로 입력해 PostgreSQL을 start한다.

pg_ctl -D /usr/local/var/postgres start

필자의 경우 이 곳 에서 sample database를 받았다.
dvdrental이라는 database에 tar 파일을 load해야 하는데, 그 전에 다음 명령어를 입력하여 role을 추가해준다.

/usr/local/opt/postgres/bin/createuser -s postgres

그런 다음, db를 생성하고 tar 파일을 load한다.
나의 경우는 db이름은 dvdrental, user이름은 postgres이었다.

createdb [db이름]
pg_restore -U [user이름] -d [db이름] [tar파일경로]

이제 dvdrental을 사용할 수 있다.

psql dvdrental

테이블의 목록을 보기 위해서는 \dt를 입력하면 되는데 MySQL에서는 볼 수 없었던 명령어라 신기했다.