I recently got a great suggestion from a reader over on Medium about sharing my workflow tools when hacking on SQL (mainly PostgreSQL, but sometimes MySQL also), hence here we are in this blog post.
As of this writing, here are the Tools/Utilities I use:
- psql – Heavy use of the
\emeta-command, which opens the current query buffer in your editor (Vim for me). See here in the official documentation about configuring your preferred editor.
- Sublime Text 3 – Some plugins I have installed are: ImprovedSQL, SqlBeautifier, SQLExec, SQLPlus, SQLTools
My typical work flow depends heavily on what I am doing. For one-off and ad-hoc queries, I typically use the psql command-line and its provided functionality – e.g., command history, meta-commands, Emacs-like movement and shortcuts:
When larger queries wind up active in the buffer, then I usually jump straight to Vim with the
\e meta-command and work on them there. Especially if I am receiving errors or incorrect results sets.
.sql source files, temp tables, CTE’s, transactions, numerous aliased table’s from mult-table joins, I lean on Sublime Text 3.
Mainly for the nice suggestions and auto-completions it gives you there (although Vim offers some too). To run individual queries from Sublime, I yank them and paste them into the
\e opened Vim buffer back over in psql for execution.
For source files composed of transactions, PLpgSQL functions, multiple SQL statements, and
INSERT statements with many comma-separated
VALUES, I’ll just execute the file with the
\i meta-command in psql.
Interested in handy psql meta-commands with example uses?
Have a look at these blog posts I have written:
- Utility psql command for saving query output to a file.
- Two handy examples of the psql \copy meta-command
- 3 command knockout combo for your psql command-line session
- Shell excitement with the psql \! meta-command
- Use shortcuts in psql to make your life easier with the \set meta-command.
Additional informative reading:
- Setting up psql, the PostgreSQL CLI (Great blog post here.)
I hope this blog post has given you some ideas for tools and utilities you can incorporate into your daily SQL work flow.
Now that I’ve shared mine, I’d love to know what you use in your SQL hacking work flow, so feel free to share in the comments below.
Thanks for reading.
Be sure and visit the stellar on-line PostgreSQL 10 Documentation for any related questions.
A Call To Action!
Thank you for taking the time to read this post. I truly hope you discovered something interesting and enlightening. Please share your findings here, with someone else you know who would get the same value out of it as well.
Visit the Portfolio-Projects page to see blog post/technical writing I have completed for clients.
Be sure and visit the “Best Of” page for a collection of my best blog posts.
Josh Otwell is an open-source SQL database enthusiast with a passion to study and grow as a SQL Developer and blogger. Other favorite activities find him with his nose buried in a good book, article, or the Linux command line. Among those, he shares a love of tabletop RPG games, reading fantasy novels, and spending time with his wife and two daughters.
Disclaimer: The examples presented in this post are hypothetical ideas of how to achieve similar types of results. They are not the utmost best solution(s). The majority, if not all, of the examples provided are performed on a personal development/learning workstation-environment and should not be considered production quality or ready. Your particular goals and needs may vary. Use those practices that best benefit your needs and goals. Opinions are my own.