PostgreSQL
Postgres 스니펫 모음입니다! 자주 잊어버리는 것들을 정리해 둔 레퍼런스 페이지입니다.
pg_ctl init -D path-path에 새로운 데이터베이스와 설정을 초기화pg_hba.conf- 호스트 기반 인증 설정pg_ident.conf- 시스템 사용자를 데이터베이스 사용자로 매핑postgresql.conf- 그 밖의 모든 설정 변경
배운 점
- 인덱스 없이 테이블을 만든 뒤 데이터를 복사하고 나중에 인덱스를 추가하는 것이 더 빠릅니다
- SSD 기반 데이터베이스에서는
random_page_cost=1.1을 사용하면 훨씬 더 잘 동작합니다
스니펫
명령어
\l데이터베이스 목록 보기\c dbname현재 사용자로 데이터베이스에 접속
다른 테이블을 복사해 테이블 생성하기
create table new_table as table old_table;참고: 이 명령은 모든 데이터를 복사하지만 인덱스나 제약 조건은 복사하지 않습니다
데이터 없이 복사하려면
create table new_table as table old_table with no data;쿼리/필터링을 적용하려면:
create table new_table as (select * from old_table where some_condition);대기 중인 락 확인하기
select relation::regclass, * from pg_locks where not granted;데이터베이스 크기 확인하기
SELECT pg_size_pretty(pg_database_size('database name'));테이블 크기 확인하기
SELECT pg_size_pretty(pg_relation_size('records'));리플리케이션 슬롯 모니터링
SELECT * FROM pg_replication_slots;리플리케이션 지연 모니터링
SELECT extract(epoch from now() - pg_last_xact_replay_timestamp()) AS replica_lag데이터베이스 스키마만 덤프하기
pg_dump --schema-only databasename유용한 도구
글을 무작위로 읽기