CYBERTEC Logo

From PostgreSQL directly to Vim

09.2013 / Category: / Tags: |

Some (obvious) ideas can struck you when you are just sitting around at the airport or so. This is exactly what happened to me yesterday in Berlin.

In some cases it can be quite handy to dump a (reasonably) small database, edit it with vi and use replay it. As a passionate (and fundamentalist) user of Vim I am always interested in finding new tweaks to make my daily work even more efficient.

Importing data from PostgreSQL to vim

Let us start with a simple table:

We can start vim now and fetch the data directly from PostgreSQL. The ! command will add arbitrary shell output to your current document. It works like this:

To start vim we use: vim /tmp/output.txt

Inside vim we can now use a : to get started:

The easiest way to get output is to run COPY and send the data to standard output. To execute COPY we have to call psql and use -c to pass a shell command to it.

Your vim document will now contain those two rows we have added before.
Of course, typing this all over again is somewhat boring. To do this in a fancier way, we can come up with a vim function like this:

Put this into .vimrc.
Calling the vim macro is also pretty simple:

The content of the table will appear in your document.

Writing back to PostgreSQL

Writing those changes back to the DB is a little bit more complicated. Have a look at another blogpost for more details.

Comments are closed.

CYBERTEC Logo white
CYBERTEC PostgreSQL International GmbH
Römerstraße 19
2752 Wöllersdorf
Austria

+43 (0) 2622 93022-0
office@cybertec.at

Get the newest PostgreSQL Info & Tools


    This site is protected by reCAPTCHA and the Google Privacy Policy & Terms of Service apply.

    ©
    2024
    CYBERTEC PostgreSQL International GmbH
    phone-handsetmagnifiercrosscross-circle linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram