How to use PostgreSQL in VS Code
Although I use Jetbrain’s Datagrip as my personal SQL IDE, I know it’s not everyone’s cup of tea and the main steam SQL IDEs like Navicat, SQLGate, and Datagrip are expensive, free SQL IDEs like DBeaver, HeidiSQL and OmniDB are good options but have different drawbacks, either being too bulky and slow, too buggy, or too lightweight. PostgreSQL’s official software Pgadmin probably has the most features, but it may be too much for this class, as I am expecting doing only simple query work.
PostgreSQL is a popular open-source relational database management system (RDBMS) that is widely used in web applications. Visual Studio Code (VS Code) is a powerful code editor that provides a wide range of functionalities, including support for various programming languages and extensions. In this article, I will guide you on how to use PostgreSQL in VS Code, including instructions on how to install the Remote SSH and PostgreSQL extensions, and how to use these 2 extensions.
Step 1: Install Remote SSH Extension
The first step in using PostgreSQL in VS Code is to install the Remote SSH extension. This extension allows you to connect to a remote server or a virtual machine (VM) using SSH. To install the Remote SSH extension, follow the steps below:
- Open VS Code.
- Click on the Extensions icon in the left-hand sidebar.
- Type
Remote SSH
in the search bar and pressEnter
. - Click on the
Install
button next to theRemote SSH
extension.
Once you have installed the Remote SSH extension, you can use it to connect to a remote server or a VM that has PostgreSQL installed.
Step 2: Connect to the Remote Server
To connect to the remote server, follow the steps below:
- Open the Command Palette by pressing
Ctrl + Shift + P
(Windows/Linux) orCmd + Shift + P
(Mac). - Type
Remote-SSH: Connect to Host
in the search bar and pressEnter
. - Click on
+ Add New SSH Host
. - Enter you MCECS account user name (i.e.,
[email protected]
) and hit enter to save into your local SSH config file.
You now should have added the SSH host like the below image, hover onto the linux.cs.pdx.edu
on the left side panel click on the left arrow to open in current windows.
After we have entered in the remote SSH host, it should prompt you to enter your password for your MCECS account like below image.
It should then prompt you to select the platform of your remote host, which you want to select Linux
.
You have now successfully connected into our school server!
Step 3: Install PostgreSQL Extension
The next step is to install the PostgreSQL extension. This extension provides support for PostgreSQL, including syntax highlighting and database exploration. To install the PostgreSQL extension, follow the steps below:
- Click on the Extensions icon in the left-hand sidebar.
- Type
PostgreSQL
in the search bar and pressEnter
. - Click on the
Install
button next to thePostgreSQL
extension.
Make sure you’re installing the extension made by Chris Kolkman. Once you have installed the PostgreSQL extension, you can connect to a database and start using PostgreSQL in VS Code.
You may also want to install CodeSnap
extension can used to export your code to an image file or copy the code snippet image into your clipboard. The Genie AI
extension is a useful extension for coding using ChatGPT. The One Dark Pro
extension offers a nice dark theme with rich syntax highlighting for various languages.
Step 4: Connect to the PostgreSQL Database
To connect to the school PostgreSQL database, follow the steps below:
- Open the Command Palette by pressing
Ctrl + Shift + P
(Windows/Linux) orCmd + Shift + P
(Mac). - Type
PostgreSQL: Add Connection
in the search bar and pressEnter
. Or click on the+
button next toPOSTGRESQL EXPLORER: POSGRESQL:
like below image. - Enter the connection details for the PostgreSQL database by following the prompt, including
- the hostname (i.e.,
dbclass.cs.pdx.edu
), - root/username (i.e.,
s23bdbxxxx or s23adbxxxx
), - password,
- port, default port
5432
is fine for local database management, - select
Use Secure Connection
, - enter your database name, which should be same as your username,
- optional, give any name you want this DB to be called.
- the hostname (i.e.,
- Once you have entered the connection details, VS Code will connect to the database and display a list of databases in the left side panel.
- Restart VSCode for the auto completion to work.
Step 5: Use PostgreSQL in VS Code
Once you have connected to the PostgreSQL database, you can start using it in VS Code. You can create, edit, and execute SQL queries, as well as manage database objects such as tables, views, and indexes.
To execute a SQL query, follow the steps below:
- Right click on the database and select
New Query
. - This should open up a new empty query on the right panel titled
Untitled-1
, you can now start typing your query works on the right side panel. - Select your query code, right click and click on
Run Query
, or use its shortcut key. - You can modify the shortcut key by pressing
Ctrl + Shift + P
(Windows/Linux) orCmd + Shift + P
(Mac) to open up the command Palette, search forPreference: Open Keyboard Shortcuts
, change it to your desired keybinding for theRun Query
command. - By clicking on the save icon on the top left corner of your table panel, this extension give you the option to export your tables into
json
,xml
,csv
format. - Once you’re done, hit
Ctrl + S
(Windows/Linux)Cmd + S
(Mac) to save your query work into.pgsql
or.sql
file at any location of your device.
Note: Every time you perform CUD (Create, Update, and Delete.) operations on DB, the downside of this extension is it does not show your new table changes immediately. You have to right click on the DB and select Refresh Items
.
You can also create a new SQL file in your local drive, follow the steps below:
- Click on the File menu in the top-left corner of the screen. Select
New File
or typeCtrl + N
(Windows/Linux)Cmd + N
(Mac). - Select the language
sql
, start your SQL query works in the new file. - Once you’re done, hit
Ctrl + S
(Windows/Linux)Cmd + S
(Mac) to save your work into.pgsql
or.sql
file at any location of your device.