Connect VS Code to PostgreSQL with SQLTools (Step-by-Step)
Connect Visual Studio Code to PostgreSQL using SQLTools in 4 steps: install drivers, configure host/port credentials, fix search_path errors, and run queries.
Most beginners start learning SQL in web playgrounds with immediate "Run" buttons. But on production analytics teams and in real-world portfolio projects, you are handed raw database credentials (host, port, user, password) and expected to query a live database, save queries into version-controlled .sql files, and deliver documented business findings.
This guide walks you through setting up Visual Studio Code with the SQLTools extension as your all-in-one data analytics workstation.
Why VS Code is the Ideal Workbench for Data Analysts
Instead of juggling three separate tools (a database GUI like DBeaver, a text editor for notes, and a Git terminal), VS Code lets you handle the entire analytical lifecycle in one interface:
Step 1: Install VS Code and the Required Extensions
If you haven't already, download and install Visual Studio Code.
Once installed:
- Open VS Code.
- Open the Extensions View by clicking the Extensions icon on the left Activity Bar or pressing
Ctrl+Shift+X(Windows/Linux) orCmd+Shift+X(macOS). - Search for and install these two extensions:
- SQLTools (by Matheus Teixeira)
- SQLTools PostgreSQL/Cockroach Driver (by Matheus Teixeira)
[!NOTE] SQLTools is modular. You need both the core extension and the PostgreSQL driver plugin for Postgres connections to work.
Step 2: Configure Your Database Connection
Once both extensions are installed, you will see a new Database icon (SQLTools) on the left Activity Bar.
- Click the Database icon in the left sidebar.
- Under the Connections panel, click Add New Connection (or the plug icon with a
+). - Select PostgreSQL from the list of database drivers.
Field-by-Field Connection Settings
Fill out the connection form using your database credentials (if you are doing a Topfolio project, grab these from your workspace's Credentials tab):
| Field | Value / Explanation | Example Value |
|---|---|---|
| Connection name | A human-readable name for your connection | Analytics DB |
| Connect using | Select Server and Port | Server and Port |
| Server Address | The database host address (labeled Host / Server Address in your credentials panel) | db.example-analytics.com |
| Port | PostgreSQL default port | 5432 |
| Database | Database name to connect to | postgres |
| Username | Your database username | read_only_user |
| Password | In the Password dropdown, select Save as plaintext in settings and paste your password into the input field | your_secret_password |
| Use SSL | Set to Disable (or leave disabled) | Disable |
- Click Test Connection at the bottom. You should see a green notification: "Connection test successful!"
- Click Save Connection.
- In the sidebar under Connections, click on your saved connection (or click the plug icon next to it) to open the active session.
Step 3: The PostgreSQL Schema Gotcha (SET search_path TO ecom;)
In multi-tenant or enterprise PostgreSQL databases, tables are grouped into schemas (e.g. ecom, saas, analytics) rather than the default public schema.
If you connect and immediately run:
SELECT * FROM orders LIMIT 10;You will receive an error:
ERROR: relation "orders" does not existThe Fix: Set Your Search Path
Always put this statement at the very top of your .sql file or run it once in your session:
SET search_path TO ecom;After executing SET search_path TO ecom;, PostgreSQL will automatically look inside the ecom schema, and queries like SELECT * FROM customers; or SELECT * FROM orders; will resolve instantly without needing ecom. prefixes!
Step 4: Writing, Executing & Inspecting Queries
- In VS Code, open your project folder (e.g.
ecommerce-sales-analysis-sql). - Open or create a SQL file in your folder, such as
sql/01_exploration.sql. - At the top right of the editor, ensure the active connection is selected (e.g.,
Topfolio Ecom). - Type your SQL queries:
-- 01_exploration.sql
-- Step 1: Set schema search path
SET search_path TO ecom;
-- Step 2: Discover all tables in the ecom schema
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'ecom'
ORDER BY table_name;
-- Step 3: Check order volume and date range
SELECT
COUNT(*) AS total_orders,
COUNT(DISTINCT customer_id) AS total_customers,
MIN(order_date) AS earliest_order,
MAX(order_date) AS latest_order
FROM orders;Keyboard Shortcuts for Running Queries
- Run Current Query: Place your cursor on the query and press
Ctrl+E(Windows/Linux) orCmd+E(Mac). - Run Entire File: Press
Ctrl+Shift+E/Cmd+Shift+E. - CodeLens Click: You can also click the small "Run on active connection" text floating above each query block.
The results grid will open in a new tab right inside VS Code, allowing you to sort columns, inspect data types, and copy outputs.
Step 5: Documenting Findings in Markdown (findings/*.md)
As a data analyst, writing the query is only 50% of the job. Communicating the business insight to stakeholders is the other 50%.
In VS Code:
- Create your findings file:
findings/01_business_overview.md. - Write your analysis using Markdown headings, bullet points, and tables.
- Open live preview by pressing
Ctrl+Shift+V/Cmd+Shift+V(orCtrl+K Vfor side-by-side preview).
# Business Overview & Scale Analysis
## 1. Executive Summary
* **Total Customer Base:** 12,450 unique customers across 3 active years (2023–2025).
* **Order Volume:** 48,210 completed transactions totaling $4.2M in gross merchandise value.
* **Key Observation:** The top 20% of customers account for 68% of total revenue.Step 6: Committing & Pushing to GitHub Inside VS Code
Once your .sql files and .md reports are ready:
- Open the Source Control tab (
Ctrl+Shift+G/Cmd+Shift+G). - Click + (Stage All Changes).
- Type a descriptive commit message (e.g.
feat: add milestone 1 exploration queries and findings). - Click Commit (or checkmark), then click Sync Changes / Push.
- Your public GitHub repository is updated, ready for mentor review!
Summary Checklist
- VS Code installed with SQLTools + PostgreSQL Driver.
- Connection saved with host, port
5432, user, password, and SSL disabled. -
SET search_path TO ecom;included at top of SQL files. - Queries executed via
Ctrl+E/Cmd+Eand results inspected in grid. - Findings documented in
findings/*.mdwith Markdown preview. - Changes committed and pushed to GitHub via VS Code Source Control.
Build Real SQL Portfolio Projects in VS Code
Apply your local VS Code and PostgreSQL setup to production datasets with mentor code reviews in the Topfolio Data Analyst Career Track.
View Guided SQL ProjectFrequently Asked Questions
Why use VS Code + SQLTools instead of DBeaver or pgAdmin?
VS Code serves as a single unified workbench: you run SQL queries, view result tables, write findings in Markdown, and commit/push changes to GitHub all within a single window without switching applications.
What driver do I select in SQLTools for PostgreSQL?
Install the official 'SQLTools PostgreSQL/Cockroach Driver' extension alongside SQLTools. When adding a connection, choose 'PostgreSQL'.
Why do I get 'relation orders does not exist' when connecting to PostgreSQL?
In databases where tables reside in a custom schema (like 'ecom'), PostgreSQL defaults to the 'public' schema. Add 'SET search_path TO ecom;' at the top of your script or qualify tables as 'ecom.orders'.
How do I run a query in VS Code using SQLTools?
Open a .sql file, select the query (or leave cursor on it), and press Ctrl+E (Windows/Linux) or Cmd+E (Mac), or click 'Run on active connection' above the query block.

Written by
Founder at Topfolio with 6+ years in data & analytics across JPMC, Ultrahuman, and high-growth startups. Sat on hiring panels, reviewed 500+ resumes, and writes practical SQL & data guides.
Related Articles
CREATE TABLE in MySQL: Syntax, Data Types & Constraints Guide
Master CREATE TABLE in MySQL with syntax examples, primary keys, foreign keys, AUTO_INCREMENT, constraints, and InnoDB engine best practices.
DDL SQL Commands: Complete Guide to Data Definition Language
Master DDL SQL commands: CREATE, ALTER, DROP, TRUNCATE, and RENAME with practical syntax, schema constraints, and DDL vs DML comparisons.
Delete Duplicate Records in SQL: 3 Proven Methods with Examples
Learn how to delete duplicate records in SQL using ROW_NUMBER() CTEs, self-joins with MIN/MAX IDs, and safe transaction workflows across dialects.