[postgresSQL] 데이터베이스, 역할, 스키마 생성

LSHan·2023년 4월 11일
1

DB

목록 보기
1/2
post-thumbnail

Postgresql로 작업할 때마다 스키마 생성부터 계속 반복적으로 검색하는 경우가 잦아 이제 나를 위해 몇 개 내용을 공부겸 나중을 위해 작성해봄.

DB접속방법

🔷 postgresSQL 리눅스 접속

[root@server ~] psql -U postgres
psql (버전)
Type "help" for help.
postgres=# 

DB생성방법

🔷 데이터베이스 목록 확인

postgres=# \l

   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileg
es
-----------+----------+----------+-------------+-------------+------------------
-----
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres
           |          |          |             |             | postgres=CTc/postgres

🔷 생성된 데이터베이스에 연결

postgres=# \c [db name]
You are now connected to database "[db name]" as user "postres".
db_name=# 

DB 계정 생성 방법

🔷 postgresSQL DB 생성/ 계정 설정

CREATE DATABASE [db name];
CREATE DATABASE [db name] OWNER [user name];

🔷 postgresSQL DB 계정주 변경

ALTER DATABASE [db name] OWNER TO [user name];

🔷 DB계정 목록 확인

SELECT * FROM PG_USER;
 usename  | usesysid | usecreatedb | usesuper | userepl | usebypassrls |  passwd  | valuntil | useconfig
----------+----------+-------------+----------+---------+--------------+----------+----------+-----------
 postgres |       10 | t           | t        | t       | t            | ******** |          |

🔷 DB계정 역할 확인

postgres=# \du

 Role name |                         Attributes                         | Member of
-----------+------------------------------------------------------------+-----------
 postgres  | Superuser, Create role, Create DB, Replication, Bypass RLS | {}

🔷 DB계정 삭제

DROP ROLE [user name];

🔷 DB계정 생성

CREATE USER [user name] PASSWORD 'password';

🔷 DB계정 password 변경

ALTER ROLE [user name] LOGIN password 'passwrd';

DB 스키마 생성 방법

🔷 DB 스키마 목록

db_name=# \dn

  List of schemas
  Name  |  Owner
--------+----------
 public | postgres
(1 rows)

🔷 DB 스키마 생성

CREATE SCHEMA [schema name] AUTHORIZATION [user name];
profile
잊으면 안될 내용들 혹은 간혹 필요한 정보들을 적습니다.

0개의 댓글