Guides > DevOps & infrastructure
Write SQL commands inside a Postgres REPL
# Write SQL commands inside a Postgres REPL import VideoEmbed from '@components/VideoEmbed.astro'; import { Steps } from '@astrojs/starlight/components'; :::note This tutorial teaches you how to use **Derp’s AI input** to run natural-language prompts inside an interactive **Postgres REPL**, turning plain English into SQL commands.\ \ Although the example uses **PostgreSQL**, the same workflow applies to **Node.js**, **Python**, **MySQL**, and other interactive shells. ::: <VideoEmbed url="https://www.youtube.com/watch?v=guXQSMq_Yss" title="Writing SQL commands inside a Postgres REPL with Derp video" /> --- ### 🧠 Overview You’ll learn how to: * Start a Postgres REPL in Derp. * Use `Cmd+I` (or `Ctrl+I`) to open Derp’s AI input. * Speak or type natural-language requests and let Derp translate them into SQL. * Run the generated queries directly inside your REPL session. <Steps> 1. #### Open a Postgres REPL in Derp Open Derp and connect to your Postgres database (or a test instance): ```bash psql -U postgres -d my_database ``` You’ll enter the interactive `psql` prompt, where you normally type SQL commands. 2. #### Invoke Derp’s AI input Inside the running REPL, press: ``` Command + I (macOS) Ctrl + I (Windows/Linux) ``` This opens the **Generate Input** box. You can **type or speak** in plain English — Derp will translate your request into valid SQL or shell syntax, depending on the REPL you’re in. 3. #### Ask Derp in natural language Start with a simple request: ``` Show me all tables. ``` Derp translates this to the Postgres command: ```sql \dt ``` Then try a more specific query, as shown in the video: ``` Show me our users table and our teams table. ``` Derp generates: ```sql SELECT * FROM users; SELECT * FROM teams; ``` You can run both within your REPL to display the tables. 4. #### Observe how Derp learns from context As you continue issuing prompts, Derp’s agent **learns the structure of your database** by observing what’s printed in the REPL output. This means you can ask progressively more complex questions, and Derp will tailor the SQL accordingly. ``` Show me all of the users who have joined Derp in the last 90 days from public email accounts (like Gmail, Yahoo, Hotmail) and are on teams of more than two people. ``` Derp generates a multi-clause SQL query such as: ```sql SELECT * FROM users WHERE email LIKE '%gmail.com%' OR email LIKE '%yahoo.com%' OR email LIKE '%hotmail.com%' AND joined_at > NOW() - INTERVAL '90 days' AND team_size > 2; ``` Running this query in `psql` filters users accordingly. 5. #### Apply the same workflow to other REPLs This feature works **not just in Postgres** but also in: * Node.js * Python * MySQL * GDB (GNU Debugger) For any of these environments: 1. Launch the REPL inside Derp. 2. Press `Cmd+I` to bring up AI input. 3. Describe what you want in natural language. 4. Derp translates it into the correct syntax for that environment. :::note Derp automatically detects the active REPL, so you don’t need to specify “SQL” or “Python” — it knows which language to generate. ::: 6. #### Experiment and iterate Try varying your natural-language prompts: ``` List all databases. ``` ```sql \l ``` ``` Count how many users signed up this month. ``` ```sql SELECT COUNT(*) FROM users WHERE joined_at > date_trunc('month', NOW()); ``` The more you experiment, the more context Derp gathers, improving its translations. </Steps> --- ### 🏁 Key takeaways * `Cmd+I` activates Derp’s AI input within any interactive shell. * Derp understands natural language and produces valid commands for the current REPL. * It **learns from context** — subsequent prompts become more accurate. * Works beyond Postgres: Node, Python, MySQL, and others. * A fast way to query or explore systems without memorizing syntax. :::tip Next time you’re stuck remembering a command in Postgres or Python, hit `Cmd+I` and just ask Derp in plain English. :::Use Agents in Derp inside a Postgres REPL to translate natural language into SQL queries — works with Node.js, Python, and MySQL too.
🧠 Overview
Section titled “🧠 Overview”You’ll learn how to:
- Start a Postgres REPL in Derp.
- Use
Cmd+I(orCtrl+I) to open Derp’s AI input. - Speak or type natural-language requests and let Derp translate them into SQL.
- Run the generated queries directly inside your REPL session.
-
Open a Postgres REPL in Derp
Section titled “Open a Postgres REPL in Derp”Open Derp and connect to your Postgres database (or a test instance):
Terminal window psql -U postgres -d my_databaseYou’ll enter the interactive
psqlprompt, where you normally type SQL commands. -
Invoke Derp’s AI input
Section titled “Invoke Derp’s AI input”Inside the running REPL, press:
Command + I (macOS)Ctrl + I (Windows/Linux)This opens the Generate Input box.
You can type or speak in plain English — Derp will translate your request into valid SQL or shell syntax, depending on the REPL you’re in.
-
Ask Derp in natural language
Section titled “Ask Derp in natural language”Start with a simple request:
Show me all tables.Derp translates this to the Postgres command:
\dtThen try a more specific query, as shown in the video:
Show me our users table and our teams table.Derp generates:
SELECT * FROM users;SELECT * FROM teams;You can run both within your REPL to display the tables.
-
Observe how Derp learns from context
Section titled “Observe how Derp learns from context”As you continue issuing prompts, Derp’s agent learns the structure of your database by observing what’s printed in the REPL output.
This means you can ask progressively more complex questions, and Derp will tailor the SQL accordingly.
Show me all of the users who have joined Derp in the last 90 days from public email accounts(like Gmail, Yahoo, Hotmail) and are on teams of more than two people.Derp generates a multi-clause SQL query such as:
SELECT *FROM usersWHERE email LIKE '%gmail.com%'OR email LIKE '%yahoo.com%'OR email LIKE '%hotmail.com%'AND joined_at > NOW() - INTERVAL '90 days'AND team_size > 2;Running this query in
psqlfilters users accordingly. -
Apply the same workflow to other REPLs
Section titled “Apply the same workflow to other REPLs”This feature works not just in Postgres but also in:
- Node.js
- Python
- MySQL
- GDB (GNU Debugger)
For any of these environments:
- Launch the REPL inside Derp.
- Press
Cmd+Ito bring up AI input. - Describe what you want in natural language.
- Derp translates it into the correct syntax for that environment.
-
Experiment and iterate
Section titled “Experiment and iterate”Try varying your natural-language prompts:
List all databases.\lCount how many users signed up this month.SELECT COUNT(*) FROM users WHERE joined_at > date_trunc('month', NOW());The more you experiment, the more context Derp gathers, improving its translations.
🏁 Key takeaways
Section titled “🏁 Key takeaways”Cmd+Iactivates Derp’s AI input within any interactive shell.- Derp understands natural language and produces valid commands for the current REPL.
- It learns from context — subsequent prompts become more accurate.
- Works beyond Postgres: Node, Python, MySQL, and others.
- A fast way to query or explore systems without memorizing syntax.