실제 DB 검증PostgreSQL 17.12에서 재현·해결 확인 · 2026-11-29 · 컨테이너 이미지 postgres:17
PostgreSQL은 개체 사이의 의존 관계를 기록해 두고, 다른 개체가 쓰고 있는 건 지우지 못하게 해요. DETAIL 줄에 무엇이 의존하는지 정확히 나오니 먼저 그걸 보세요.
HINT는 DROP ... CASCADE를 권하는데, 이러면 뷰나 외래 키 제약 같은 의존 개체도 함께 지워져요. 아래 해결법은 무엇이 지워지는지 알 수 있게 의존 개체를 하나씩 처리해요.
01뷰가 쓰는 테이블을 DROP
뷰 v_order_total이 orders를 조회해요. DROP TABLE orders가 실패하고 DETAIL에 view v_order_total depends on table orders가 나와요.
사전 준비
CREATE TABLE orders (id int PRIMARY KEY, amount numeric);
CREATE VIEW v_order_total AS SELECT sum(amount) AS total FROM orders;CREATE TABLE
CREATE VIEWDROP TABLE orders;ERROR: 2BP01: cannot drop table orders because other objects depend on it
DETAIL: view v_order_total depends on table orders
HINT: Use DROP ... CASCADE to drop the dependent objects too.SELECT view_name FROM information_schema.view_table_usage WHERE table_name = 'orders'; view_name
---------------
v_order_total
(1 row)DROP VIEW v_order_total;DROP VIEWinformation_schema.view_table_usage로 테이블을 쓰는 뷰를 찾을 수 있어요(여기선 v_order_total). 뷰를 먼저 지우거나 고치면 DROP TABLE orders가 성공해요.
DROP TABLE orders;DROP TABLE02뷰가 조회하는 컬럼을 DROP
뷰 v_customers가 fax 컬럼을 조회해요. 컬럼만 지우면 view v_customers depends on column fax of table customers로 실패해요.
사전 준비
CREATE TABLE customers (id int PRIMARY KEY, name text, fax text);
CREATE VIEW v_customers AS SELECT id, name, fax FROM customers;CREATE TABLE
CREATE VIEWALTER TABLE customers DROP COLUMN fax;ERROR: 2BP01: cannot drop column fax of table customers because other objects depend on it
DETAIL: view v_customers depends on column fax of table customers
HINT: Use DROP ... CASCADE to drop the dependent objects too.BEGIN;
DROP VIEW v_customers;
ALTER TABLE customers DROP COLUMN fax;
CREATE VIEW v_customers AS SELECT id, name FROM customers;
COMMIT;BEGIN
DROP VIEW
ALTER TABLE
CREATE VIEW
COMMIT트랜잭션 하나로 뷰 삭제 → 컬럼 삭제 → fax 없이 뷰 다시 만들기를 하세요. 끝나면 customers에는 id, name만 남고 뷰도 그대로 있어요.
SELECT column_name FROM information_schema.columns WHERE table_name = 'customers' ORDER BY ordinal_position; column_name
-------------
id
name
(2 rows)03외래 키가 참조하는 부모 테이블을 DROP
orders.customer_id가 customers를 참조해요. DETAIL에 제약 이름이 나와요: constraint orders_customer_id_fkey on table orders depends on table customers.
사전 준비
CREATE TABLE customers (id int PRIMARY KEY, name text);
CREATE TABLE orders (id int PRIMARY KEY, customer_id int REFERENCES customers (id));CREATE TABLE
CREATE TABLEDROP TABLE customers;ERROR: 2BP01: cannot drop table customers because other objects depend on it
DETAIL: constraint orders_customer_id_fkey on table orders depends on table customers
HINT: Use DROP ... CASCADE to drop the dependent objects too.ALTER TABLE orders DROP CONSTRAINT orders_customer_id_fkey;ALTER TABLEDETAIL의 이름으로 자식 테이블의 외래 키를 지우세요: ALTER TABLE orders DROP CONSTRAINT orders_customer_id_fkey. orders 테이블과 데이터는 그대로고, 그다음 DROP TABLE customers가 성공해요.
DROP TABLE customers;DROP TABLE04개체를 가졌거나 권한이 있는 사용자를 DROP ROLE
퇴사자 계정을 지우려고 DROP ROLE lab_b를 하면 role "lab_b" cannot be dropped because some objects depend on it이 나요. DETAIL에 스키마 lab_b 소유, 테이블 lab_b.my_report 소유, lab_a.sales 권한이 다 나와요.
사전 준비
CREATE TABLE sales (id int PRIMARY KEY, amount numeric);
GRANT SELECT ON sales TO lab_b;CREATE TABLE
GRANTCREATE TABLE my_report (id int);CREATE TABLEDROP ROLE lab_b;ERROR: 2BP01: role "lab_b" cannot be dropped because some objects depend on it
DETAIL: owner of schema lab_b
privileges for table lab_a.sales
owner of table lab_b.my_reportREASSIGN OWNED BY lab_b TO lab_a;
DROP OWNED BY lab_b;REASSIGN OWNED
DROP OWNEDREASSIGN OWNED BY lab_b TO lab_a로 소유 개체를 다른 롤에 넘기고, DROP OWNED BY lab_b로 남은 권한을 지우세요. 그러면 DROP ROLE lab_b가 성공해요. 이 두 명령은 데이터베이스 단위라, 롤이 개체를 가진 데이터베이스마다 실행해야 해요.
DROP ROLE lab_b;DROP ROLE자주 묻는 질문
cannot drop table because other objects depend on it 뜻은?
뷰, 외래 키 같은 다른 개체가 아직 그 테이블을 쓰고 있다는 뜻이에요. DETAIL 줄에 목록이 나와요. 그것들을 먼저 지우거나 고치세요. 같이 지워도 되면 CASCADE를 써요.
DROP TABLE ... CASCADE 써도 되나요?
뷰·외래 키 제약 같은 의존 개체도 함께 지워져요. 무엇이 사라지는지 DETAIL 목록을 먼저 확인하세요.
PostgreSQL 사용자(롤) 삭제가 안 돼요
REASSIGN OWNED BY 옛롤 TO 새롤;, DROP OWNED BY 옛롤;을 실행한 뒤 DROP ROLE 옛롤;을 하세요.
뷰에서 쓰는 컬럼을 지우려면?
뷰 삭제 → 컬럼 삭제 → 그 컬럼 없이 뷰 다시 생성. 트랜잭션 하나로 묶는 게 안전해요.
검증 방법
시나리오마다 새 DB 사용자를 만들어 시작해요. 스크립트가 사전 준비 → 에러 재현(에러 코드 일치 확인) → 해결 → 원래 문장 재실행 순서로 실행하고, 화면의 출력은 DB가 실제로 낸 결과를 스크립트가 그대로 저장한 거예요.
lab_a, lab_b는 테스트용 일반 역할(같은 이름의 스키마 소유), postgres는 슈퍼유저예요. PostgreSQL 17.12