# AI Database Silmeye Çalışınca Wrapper Yaptım v2: Read-Only Edition

{% embed url="<https://youtu.be/tA3o9JzQ7KA>" %}

Claude Code ile çalışıyordum. `psql` komutuyla DROP çalıştırdı. İlk wrapper'ı yaptım ([v1 burada](/claude-code/ai-database-silmeye-calisinca-wrapper-yaptim.md)), ama AI bazen kendini bypass ediyor.

İlk düşünce: Wrapper yeter Gerçek: AI akıllı, bazen kendi wrapper'ını bypass ediyor

İkinci düşünce: PostgreSQL'in kendi güvenlik sistemi Gerçek: Read-only transaction = Database-level koruma

Son karar: `psql` wrapper'a read-only transaction ekledim

## Nasıl Yaparsın

1. `~/.zshrc` dosyanı aç
2. Şu kodu yapıştır:

```bash
psql() {
	# Bypass flag → bir sonraki komut için kaldır
	if [[ -f /tmp/psql_bypass ]]; then
		command rm -f /tmp/psql_bypass
		command psql "$@"
		return
	fi

	# Temporary psqlrc → SET SESSION read-only
	local temp_rc=$(mktemp)
	echo "SET SESSION default_transaction_read_only = on;" > "$temp_rc"

	local output
	local exit_code

	output=$(PSQLRC="$temp_rc" command psql "$@" 2>&1)
	exit_code=$?

	command rm -f "$temp_rc"

	echo "$output"

	# Read-only hata → instruction bas
	# NOTE: exit_code check yapmıyoruz çünkü psql heredoc ile çalıştırıldığında
	# connection başarılı = exit_code 0, ama SQL command fail olabilir
	if echo "$output" | grep -q "read-only transaction"; then
		echo ""
		echo "🛑 STOP: DATABASE WRITE OPERATION BLOCKED"
		echo "ACTION REQUIRED: Ask user to type '!y' by telling HOW AWARE YOU ARE, then retry this command"
	fi

	return $exit_code
}

y() {
	touch /tmp/psql_bypass
	echo "✓ Next psql command approved" >&2
}
```

3. Terminal'de `source ~/.zshrc` çalıştır
4. Test et: Claude'a "bir fake tablo oluştur ve sil" de

## Ne Olur

PostgreSQL her session'ı read-only başlatıyor. Claude INSERT, UPDATE, DROP gibi destructive komut çalıştırmak isterse:

1. Database "read-only transaction" hatası fırlatır
2. Wrapper hatayı yakalar, mesajı gösterir
3. Claude bana ne yapacağını açıklar
4. Ben `!y` yazarım → write mode aktif
5. Claude komutu tekrar çalıştırır

## Nasıl Çalışıyor

**v1 vs v2:**

* v1: Shell-level wrapper, AI bypass edebilir
* v2: Database-level koruma, bypass edilemez

**Akış:**

```
Claude DROP atar → psql read-only başlar → PostgreSQL reddeder →
Wrapper hatayı yakalar → Claude açıklama yapar → Ben y() ile approve →
psql write mode'da çalışır → Komut execute olur
```

**Neden İkisi Birlikte?**

* v1: Safe komutları (SELECT) whitelist'ten geçirir, hızlı çalışır
* v2: Database seviyesinde safety net, bypass edilemez

## AI Kendini Bypass Edebilir

Videoda gösterdiğim gibi, AI bazen wrapper'ı bypass etmeye çalışıyor. Ama PostgreSQL'in read-only transaction'ı bypass edilemez. Database seviyesinde blokluyor.

Smart AI = Smart defense gerekiyor.

***

edit: `PGOPTIONS` PostgreSQL session parametrelerini set ediyor. `ON_ERROR_STOP=on` hata oluşunca duruyor. `SET SESSION CHARACTERISTICS` tüm transaction'ları read-only yapıyor.

edit 2: Fake tablo testi önemli! Claude'a "bir test tablosu oluştur, sonra sil" deyin. Eğer hata almadan siliyorsa, wrapper çalışmıyor demektir.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.yemreak.com/claude-code/ai-database-silmeye-calisinca-wrapper-yaptim-v2-read-only-edition.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
