How do I query databases directly from Vim without leaving my editor?
Answer
:DB
Explanation
The vim-dadbod plugin by Tim Pope turns Vim into a powerful database client. The :DB command lets you execute SQL queries against PostgreSQL, MySQL, SQLite, MongoDB, Redis, and many other databases — all without switching to a separate terminal or GUI tool.
How it works
Provide a connection URL and a query to the :DB command:
:DB postgresql://user:pass@localhost/mydb SELECT * FROM users LIMIT 10;
The query results appear in a new scratch buffer, formatted as a readable table. You can yank, search, and navigate the results using standard Vim commands.
Setting a default connection
Instead of typing the connection URL every time, set it once per buffer or globally:
" Set for the current buffer
:let b:db = 'postgresql://user:pass@localhost/mydb'
" Set globally in your vimrc
let g:db = 'sqlite:///path/to/app.db'
With a default connection set, you can run queries without the URL:
:DB SELECT * FROM users;
Executing queries from the buffer
The real power of dadbod is running SQL directly from your editing buffer. Write your SQL in any file, then execute it:
- Current line: Place your cursor on a SQL statement and run
:DB - Visual selection: Select a block of SQL in visual mode, then run
:'<,'>DB - Entire file: Run
:%DBto execute the whole buffer as a query - Motion: Use
:DBas an operator withdb— for example,dBipexecutes the current paragraph
Supported databases
| Database | URL Format |
|---|---|
| PostgreSQL | postgresql://user:pass@host/db |
| MySQL | mysql://user:pass@host/db |
| SQLite | sqlite:///path/to/file.db |
| SQL Server | sqlserver://user:pass@host/db |
| MongoDB | mongodb://host/db |
| Redis | redis://host:port |
| Oracle | oracle://user:pass@host/sid |
| DuckDB | duckdb:///path/to/file.duckdb |
Companion plugin: vim-dadbod-ui
The vim-dadbod-ui plugin adds a visual database explorer sidebar. Run :DBUI to open a split showing:
- Saved database connections
- Database schemas, tables, and columns
- A query buffer per connection with auto-completion
- Query history that persists across sessions
Press <CR> on a table name to preview its contents, or R to refresh the schema.
Auto-completion with dadbod-completion
Install vim-dadbod-completion to get table name, column name, and keyword completion while writing SQL:
autocmd FileType sql,mysql,plsql lua require('cmp').setup.buffer({ sources = {{ name = 'vim-dadbod-completion' }} })
For Vim users without Lua, it integrates with deoplete, coc.nvim, and Vim's built-in omnifunc.
Managing multiple connections
Define connections in a dictionary for easy switching:
let g:dbs = {
\ 'dev': 'postgresql://localhost/myapp_dev',
\ 'staging': 'postgresql://staging-host/myapp_staging',
\ 'local_sqlite': 'sqlite:///tmp/test.db',
\}
With vim-dadbod-ui, these appear in the sidebar and you can switch between them with a keystroke.
Tips
- Use
.sqlfiles as scratch pads for queries — write SQL, visually select it, and run:'<,'>DBto execute just that block - Dadbod reads credentials from environment variables and
.pgpass/.my.cnffiles, so you never need to hardcode passwords - The output buffer is a regular Vim buffer — use
/to search results,yyto copy rows, or pipe through:sort - Combine with vim-dispatch by setting
let g:db_async = 1to run long queries without blocking Vim - For Neovim users, the full dadbod ecosystem (dadbod + dadbod-ui + dadbod-completion) provides an experience comparable to dedicated database GUIs like DataGrip or DBeaver