Connect to PostgreSQL from Vim

Simplifying my workflow

Going back an forth between your editor and psql is tedious. It breaks the flow of working on your task.

Editing capabilities of psql are limited. It would be much better to use full power of real text editor to craft SQL queries. An editor which excels in efficient text manipulation. Something like Vim.

dbext is a great plugin which enables you to write SQL queries directly in Vim, run them and then inspect the results. All of that without leaving the powerful Vim.

The plugin was around for a very long time. I remember using it together with MySQL 4.0 around year 2008. Back then it was already a solid piece of software.

Initial contact with the plugin may be a bit intimidating. The official page starts with profile configuration which was for me a bit confusing.

There’s really great help included with dbext. The best thing to do in the beginning is to run :he dbext-tutorial which will guide you through all the features. And there’s a lot of them.

To connect to the database, in the beginning, the easiest is to let dbext ask you for all the required parameters. Nifty shortcut sbp will ask you all the required parameters.

Once you are connected to the database you can issue SQL queries.

The plugin gives you a lot of options in that regard but I only use one: se. It stands for SQL execute and it does exactly that. It executes the SQL query on which the cursor is currently positioned. For me that’s enough.

The results are displayed in a separate window which opens automatically and which is easily dismissible with q once the window is activated.

There’s a lot more to dbext and I will explore its vast capabilities. For now being able to execute SQL queries directly from my editor is enough. I’m happy with one more reason for not having to switch to another application.