사용자 도구

사이트 도구


postgresql:home

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

양쪽 이전 판이전 판
다음 판
이전 판
postgresql:home [2024/03/02 12:36] – [PostgreSQL] taekgupostgresql:home [2025/04/15 10:05] (현재) – 바깥 편집 127.0.0.1
줄 1: 줄 1:
 +====== PostgreSQL ======
 +  * [[postgresql:configuration|설정하기]]
 +  * [[postgresql:backup|Backup]]
 +  * [[postgresql:install|Install]]
 +  * [[postgresql:descript|Table Description]]
 +  * [[postgresql:recursive|Recursive Query - Oracle의 START WITH, CONNECT BY PRIOR와 유사]]
 +  * [[postgresql:service|postgresql 서비스]]
 +  * [[postgresql:comments|Table, Column Comment보기]]
 +  * [[postgresql:backup and restore|Backup And Restore]]
 +  * [[postgresql:upgrade|Upgrade]]
 +  * [[postgresql:replication|Replication 구축하기]]
 +  * [[postgresql:apt|Apt 등록]]
 +  * [[postgresql:pg_upgrade|pg_upgrade를 이용한 Version Up]]
 +  * [[postgresql:vacuum|VACUUM]]
  
 +  * [[postgresql:psql|psql 사용하기]]
 +
 +===== Start / Stop =====
 +macOS에서 Server.app이 있으면 postgres가 기본으로 설치되어 있다.
 +9.4, 12.4 -> 9.4는 잘되는데 12.4는 어떻게 하는지 모르겠다.
 +brew을 이용하여 설치하ㅡㄴ 것을 추천한다.
 +  * initDB <code bash>
 +initdb --locale=C -E UTF-8 /usr/local/var/postgres
 +initdb --locale=ko_KR -E UTF-8 /usr/local/var/postgres
 +initdb -D /mnt/maro/postgreSQL/garam@16 --auth-local peer --auth-host scram-sha-256 --no-instructions
 +</code>
 +  * Start the PostgreSQL Server.<code bash>
 +postgres -D /usr/local/var/postgres
 +pg_ctl -D /usr/local/var/postgres start
 +pg_ctl -D /usr/local/var/postgres stop
 +pg_ctl -D /usr/local/var/postgres -l /usr/local/var/postgres/server.log start
 +</code>
 +  * Stop the PostgreSQL Server.<code bash>
 +pg_ctl -D /usr/local/var/postgres -m smart stop      # oracle shutdown
 +pg_ctl -D /usr/local/var/postgres -m fast stop       # oracle immediate
 +pg_ctl -D /usr/local/var/postgres -m immediate stop  # oracle abort
 +
 +// 서비스 중지
 +-w 작업이 끝날때 까지 기다림
 +-W 작업이 끝날때 까지 기다리지 않음
 +// stop, ,restart 때 사용할 수 있는 옵션들
 +-m 중지방법 (smart, fast, immediate) 중 하나
 + 
 +smart : 모든 클라이언트가 연결이 끊기게 되면 중지
 +fast : 클라이언트의 연결을 강제로 끊고 정상적으로 중지
 +immediate : 무조건 중지. 다시 시작할때 복구 작업을 할 수도 있음
 +
 +pg_ctl stop -mf -w -D /usr/local/var/postgres
 +pg_ctl -D /usr/local/var/postgres stop -s -m fast
 +</code>
 +==== Create  User ===
 +  * 현재 사용자 확인
 +<code sql>
 +SELECT * FROM PG_SHADOW;
 +select * from pg_user;
 +</code>
 +  * \du를 입력하면 USER들이 가지고 있는 ROLE들을 확인 할 수 있습니다. SUPERUSER인 postgres는 SUPERUSER, CREATE ROLE, CREATE DB, REPLICATION기능을 가지고 있습니다.
 +<code sql>
 +\du
 +\du+
 +</code>
 +  * SQL statement
 +<code sql>
 +SELECT u.usename AS "Role name",
 +  CASE WHEN u.usesuper AND u.usecreatedb THEN
 +            CAST('superuser, create database' AS pg_catalog.text)
 +       WHEN u.usesuper THEN
 +            CAST('superuser' AS pg_catalog.text)
 +       WHEN u.usecreatedb THEN
 +            CAST('create database' AS pg_catalog.text)
 +       ELSE CAST('' AS pg_catalog.text)
 +  END AS "Attributes"
 +FROM pg_catalog.pg_user u
 +ORDER BY 1;
 +</code>
 +  * Create User
 +<code sql>
 +CREATE SCHEMA test;
 +CREATE USER xxx PASSWORD 'yyy';
 +alter user xx password 'yyy';
 +GRANT ALL ON SCHEMA test TO xxx;
 +GRANT ALL ON ALL TABLES IN SCHEMA test TO xxx;
 +alter schema test owner to xxx;
 +alter database mydb set search_path to boen;
 +</code>
 +  * Sample
 +<code sql>
 +CREATE ROLE testuser LOGIN PASSWORD 'password';
 +CREATE TABLESPACE testtablespace OWNER testuser LOCATION '/pgdata/testdbtablespace';
 +CREATE SCHEMA testschema AUTHORIZATION testuser;
 +ALTER SCHEMA testschema OWNER TO testuser;
 +CREATE DATABASE testdb WITH ENCODING 'UNICODE' OWNER testuser;
 +ALTER DATABASE testdb SET search_path TO testschema;
 +ALTER ROLE testuser SET search_path TO testschema;
 +GRANT ALL ON DATABASE testdb TO testuser;
 +ALTER ROLE testuser SET default_tablespace = testtablespace;
 +ALTER DATABASE testdb SET default_tablespace = testtablespace;
 +</code>
 +
 +  * 사용자생성
 +<code bash>
 +$ sudo -u postgres createuser --interactive
 +</code>
 +  * 사용자생성
 +<code sql>
 +postgres=# CREATE ROLE postgres SUPERUSER CREATEDB CREATEROLE INHERIT LOGIN REPLICATION BYPASSRLS;
 +</code>
 +==== Config file ====
 +  * postgresql.conf - 가장 중요한 설정화일입니다. 할당되는 메모리량, 데이터화일의 물리적 위치, 리스닝하고 있는 IP 정보등을 설정합니다. 오라클의 init<SID>.ora 과 같은 역할
 +listen_addresses = '*'
 +  * pg_hba.conf - 데이터베이스에 접속 시도하는 유저들을 제어합니다. (쉽게 말하면 방화벽 같은 역할..비유가 적절한가..쩝). 굳이 비유하자면, 오라클에서도 sqlnet.ora 화일에서 특정 IP 를 블럭킹할수 있죠.
 +host    all             all             192.168.1.1/24          md5
 +  * pg_ident.conf - os 인증유저와 PostgreSQL 유저와의 매핑 설정을 하는 화일입니다. 근데, 잘 안쓰여요.
 +<code sql>
 +SELECT name, setting, category
 +FROM pg_settings
 +WHERE category = 'File Locations';
 +</code>
 +==== psql Remote Connect ====
 +<code bash>
 +psql -h repository.mama.kr -U postgres -d postgres
 +</code>
 +Configuring postgresql.conf
 +<code bash>
 +$ find \ -name "postgresql.conf"
 +/var/lib/pgsql/9.4/data/postgresql.conf
 +</code>
 +
 +==== Install ====
 +<code bash>
 +apt-get install postgresql
 +</code>
 +10  main    5432 down   postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log
 +
 +=== 최초 접속 ===
 +<code bash>
 +$ sudo -u postgres psql
 +</code>
 +
 +===== Database 생성 =====
 +** Database 생성 **
 +<code sql>
 +create database garam;
 +</code>
 +** Database 삭제 **
 +<code sql>
 +drop database garam;
 +</code>
 +
 +===== 접속 =====
 +  * postgresql.conf --> pg_hba.conf
 +
 +=== pg_hba.conf ===