In this tutorial, I will talk about how to connect to PostgreSQL database in VS Code. There is an extension that Microsoft just released to run SQL commands against a running PostgreSQL instance and see the result. The SQL result that it returns can be exported to an Excel, CSV, JSON, and XML file. You just install and activate the extension and then use the command palette of VS Code to connect and create database connection profile to do whatever you like.
If you use VS Code for code editing and use PostgreSQL database then now you can run SQL from the interface of VS Code itself. With the PostgreSQL plugin, you can open a SQL file or create new with some valid SQL code and simply execute it. If you use this plugin this way then you will not have to use some external PostgreSQL editors out there. Yo just create a SQL query you want to run and then simply execute it to get the results.
How to Connect to PostgreSQL Database in VS Code?
Run VS Code and then in the marketplace, simply search for “PostgreSQL” and install the extension. After installing this extension, you will see some commands in the VS Code Command Palette. Commands to connect to database, create connection profile, and execute SQL code are there.
You will only have to configure the database connection in the beginning and you can easily connect to that. Also, if you want to create multiple connection profiles then it lets you do that. However, for multiple connections, there are some problems with the default port number. If you use different port number then you will have to configure that as mentioned in this thread.
Now, write some SQL code in the editor and then activate any command. Use Ctrl+Shift+P to activate the palette and look for target PostgreSQL command you want to execute. For example, if you want to retrieve data from a tablet then simply write select SQL query and use “PostgreSQL: Execute Query” option to run it. It will open a separate tab with the output returned by it
You can run any SQL query and even import SQL files to run them and see the output. The SQL output which it produces can be exported to a file as well. It supports Excel, CSV, JSON, and XML for exporting query results on your PC.
This was all for using this VS Code extension to edit, manipulate local PostgreSQL databases. But you can use the similar approach for remote PostgreSQL databases. Just create a new connection profile and then run SQL like a pro. Also, VS Code is cross-platform so you can use this extension on whatever platform you are one with same process.
VS Code is quite popular code editor now and if you work with PostgreSQL database then you will like the newly launched PostgreSQL plugin for it. Now without switching to other software, you can run SQL queries from within VS Code. And what I liked most about it is that you get the power of Intellisense and its ability to export the query results to a variety of formats. Although, connection profiles can be tricky at first but as you get your hands on it, it will become easy for you.