View

TIL | PostgreSQL_Setting

Melody:) 2021. 10. 8. 10:55

PostgreSQL Setting

설치

$ brew install postgresql

서비스 시작

$ brew services start postgresql

psql 접속

$ psql postgres
$ psql postgres -U melody

 

psql 데이터베이스 생성

postgres=# CREATE DATABASE "test_db" WITH OWNER = test ENCODING = 'UTF8' template = template0;

데이터베이스 리스트 보기

postgres=> \list

테이블 리스트 보기

postgres=> \dt

특정 database로 연결하기

postgres=> \connect test <- 'test' DB로 연결

사용자 생성

// 사용자 생성 및 패스워드 등록

postgres=# CREATE ROLE test WITH LOGIN PASSWORD 'test!';

// 비밀번호 등록사용자 생성 및 권한부여1

postgres=# CREATE ROLE melody3 LOGIN SUPERUSER INHERIT CREATEDB CREATEROLE REPLICATION;

// 비밀번호 등록사용자 생성 및 권한부여2
postgres-# CREATE ROLE test LOGIN NOSUPERUSER INHERIT NOCREATEDB NOCREATEROLE NOREPLICATION;


// 비밀번호 등록
postgres-#  ALTER ROLE test WITH PASSWORD 'test!';

권한 부여

postgres=# ALTER ROLE test CREATEDB;

사용자 권한 확인

1. 사용자 리스트 보기

postgres=# \du

2. 추가한 사용자로 접속하기

$ psql postgres -U test
postgres=>

postgres=#postgres=> 로 바뀐 것을 확인 할 수 있음.

특정 유저에게 DB 권한 부여하기

# GRANT ALL PRIVILEGES ON DATABASE [database] TO [role(user)];


postgres=> GRANT ALL PRIVILEGES ON DATABASE test TO test;
GRANT

스키마 생성

$ CREATE SCHEMA melody

** 주의 : 권한이 있는 database로 connect를 하고 스키마를 만들어야 함 **

스키마 목록 확인 커맨드

test=> \dn

버전 변경 DownGrade

설치된 postgresq 버전들 확인

$ brew search postgresq

==> Formulae
postgresql ✔     postgresql@11    postgresql@13    postgresql@9.5   qt-postgresql
postgresql@10    postgresql@12    postgresql@9.4   postgresql@9.6   postgrest
==> Casks
navicat-for-postgresql

postgresq  제거

 brew uninstall postgresql

postgresq  13 설치

 $ brew install postgresql@13

postgresq  13 시작

$ brew services stop postgresql

$ brew services start postgresql@13

$ brew search postgresql

기존 버전과 연결 끊기

brew unlink postgresq

 버전13 과 연결 하기

brew link postgresql@13

AWS RDS 연결

psql postgres -h melodyrds.ap-northeast-1.rds.amazonaws.com -p 5432 -U username
Password for user :

Dumpfile 만들기

pg_dump -U melody -Fc 데이터베이스이름 > 파일이름.dump

'PostgreSQL' 카테고리의 다른 글

TIL | PostgreSQL_ERROR_sorry, too many clients already  (0) 2021.10.20
Share Link
reply
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31