-
Notifications
You must be signed in to change notification settings - Fork 0
PostgreSQL 설치 및 유저,데이터베이스 설정하기
PostgreSQL을 EDB 페이지를 통해 Installer를 다운받고 설치를 했었다.
설치 후 postgre(psql) command도 유효하지 않았고, /Library/PostgreSQL/12/bin 경로에 있길래 거기에 직접 들어가서 접속 시도를 해 보았다.
그런데 로그인을 하려는데 자꾸 비밀번호가 안 맞는지 접속이 안 되었다.. 내가 패키지로 설치할 때 설정한 비밀번호로 분명 입력했는데...
/Library/PostgreSQL/12/bin$ ./psql -U postgre
Password for user postgre:
psql: error: could not connect to server: FATAL: password authentication failed for user "postgre"
그래서 지우기로 했다.
stackoverflow.com: PosgreSQL 완벽히 지우는 방법
$ brew install postgresql
$ brew services start postgresql
이 포스팅 에서는, 컴퓨터 재부팅 때마다 서비스를 켜 주는 커맨드가 소개되고 있는데, 이 커맨드를 안 실행하고 한번 재부팅 해보면 어떨까 실험 해 봐야겠다
$ pg_ctl -D /usr/local/var/postgres start && brew services start postgresql
$ psql postgres
postgres=# CREATE DATABASE <databaseName>;
Postgres doesn’t actually directly manage users or groups, like most standard permission models do. Instead, it directly manages what it calls roles. reference
Role을 새로 생성하고, 거기에 권한을 부여하는 방식으로 진행한다.
postgres=# CREATE ROLE test WITH LOGIN PASSWORD 'test!';
CREATE ROLE
현존하는 Role 확인하기: SELECT rolname FROM pg_roles;
PostgreSQL Documentation: Create Role
권한 부여하기
postgres=# ALTER ROLE test CREATEDB;
지원되는 Permission모음:
- SUPERUSER | NOSUPERUSER | CREATEDB | NOCREATEDB | CREATEROLE | NOCREATEROLE | INHERIT | NOINHERIT | LOGIN | NOLOGIN | REPLICATION | NOREPLICATION | BYPASSRLS | NOBYPASSRLS
사용자 리스트 보기
$ psql postgres
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
geoseong | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
test | Create DB | {}
추가한 사용자로 접속하기
$ psql postgres -U test
psql (12.3)
Type "help" for help.
postgres=>
- 보면
postgres=#
가 아니라postgres=>
로 바뀐 것을 확인 할 수 있다. -
#
는 superuser 라는 뜻이고,>
는 superuser가 아니라는 것이다
postgres=> CREATE DATABASE test;
CREATE DATABASE
특정 유저에게 DB의 모든 권한 부여하기
GRANT ALL PRIVILEGES ON DATABASE [database] TO [role(user)];
postgres=> GRANT ALL PRIVILEGES ON DATABASE test TO test;
GRANT
postgres=> \connect test
You are now connected to database "test" as user "test".
데이터베이스 리스트 보기
postgres=> \list
테이블 리스트 보기
postgres=> \dt
특정 database로 연결하기
postgres=> \connect test <- 'test' DB로 연결