> For the complete documentation index, see [llms.txt](https://docs.yemreak.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.yemreak.com/contents/1a6063e6.md).

# 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](https://github.com/yemreak/blog/blob/main/docs/tr/contents/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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

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

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.yemreak.com/contents/1a6063e6.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
