본문 바로가기

PostgreSQL4

postgresql sequence 사용하기 postgresql의 sequence에 대해서 다음과 같은 명령어들이 있다. 다음의 가상 테이블이 있다고 가정한다. table명 : member column명 : member_id, member_name 시퀀스 생성 -- serial4를 입력하여 자동으로 시퀀스를 생성하는 방법 CREATE TABLE member ( member_id serial4 NOT NULL, member_name varchar NOT NULL, ); -- mem_seq를 수동으로 생성하고 관리하는 방법 CREATE SEQUENCE mem_seq START 1; CREATE TABLE member ( member_id int4 NOT NULL DEFAULT nextval('mem_seq'::regclass), -- 회원번호 memb.. 2023. 8. 24.
postgresql lock 테이블 조회 및 kill lock 테이블 조회 select b.relname, a.locktype, page, virtualtransaction, pid, mode, granted from pg_locks a, pg_stat_all_tables b where a.relation = b.relid order by relation asc; process kill select pg_cancel_backend(pid); 2023. 4. 19.
postgresql dump & restore - 자주 사용하는 postgresql dump 및 restore 명령어 -d DB명 -h : DB주소 -p : port번호 -U : 유저명 -F : 백업 포맷 지정 (p: plain-text, c: custom-format, d: directory, t: tar) -f : 백업 파일명 -t : 특정 테이블명 -j : Backup시 병렬처리 여부 및 그 정도 -v : 진행 과정 표시 - 이외의 다른 명령어들은 공식 홈페이지를 참조 https://www.postgresql.org/docs/current/app-pgdump.html pg_dump 예시 pg_dump -h localhost -p 5432 -d testdb -U postgres -v -F t > /backup/test_backup.tar 위의 명.. 2023. 2. 16.
postgresql에서 auto increment 설정(자동 index증가) My Sql에서는 sequence번호를 자동으로 생성해주는 auto increament 기능이 있어서 편하게 사용했다. 최근 postgrespostgresql를 관계형 DB로 사용할 기회가 생겨 테이블 스키마를 생성하던 중 해당 문제에 직면하였고 해결하였기에 기록을 하고자 글을 쓴다. 코드의 예시는 다음과 같다. CREATE SEQUENCE example_id_seq; CREATE TABLE example_table ( id integer NOT NULL DEFAULT nextval(example_id_seq) PRIMARY KEY, name varchar NOT NULL, nick varchar NOT NULL ); ALTER SEQUENCE example_id_seq OWNED BY example_t.. 2020. 6. 29.