In this tutorial, you will learn how to make SQL queries inside Visual Studio Code.
Prerequisites:
- You need to have PostgreSQL installed on your machine
To be able to use your PostgreSQL database with Visual Studio Code, I recommend the PostgreSQL Management extension from Chris Kolkman.
After you have installed the extension and set up the database connection (see documentation), you can proceed as follows:
Step 1: In Visual Studio Code, use the keyboard shortcut:
- Mac: Command+Shift+P
- Windows: Ctrl+Shift+P
and select PostgreSQL: New Query
(this will open a blank file).
Step 2: Next, you need to select a Postgres Server (click on the icon Select Postgres Server
) in the side bar (at the bottom) of the Visual Studio Code user interface. Select the server of your choice and choose the default database postgres
.
Step 3: Now you could write SQL statements inside the blank file and run them (using the right mouse click).
Step 4: In Visual Studio Code click on the PostgreSQL Explorer (icon with an elephant) in the activity bar at the far left-hand side. Use your right mouse and click on refresh your database (in my case postgres
) to see all tables.
Note: if you choose a Postgres server (Step 2) before you open a script (Step 1), you may get an error meassage: unable to select a connection - a document is not active
. If this is the case, simply start with Step 1 and you should be fine.