Setup Local DB

πŸš€ Goal

  • Install PostgreSQL locally

  • Connect it to VS Code

  • Run SQL queries and manage your database


🧠 Prerequisites


πŸ–₯️ On macOS

1. Install PostgreSQL via Homebrew

brew install postgresql
brew services start postgresql

2. Create a User & Database

psql postgres

-- Inside the prompt:
CREATE USER myuser WITH PASSWORD 'mypassword';
CREATE DATABASE mydb OWNER myuser;
\q

3. Install VS Code PostgreSQL Extension

Open VS Code and install:

πŸ” PostgreSQL by Microsoft

Or from terminal:

4. Connect to PostgreSQL in VS Code

  1. Open Command Palette (Cmd+Shift+P)

  2. Run PostgreSQL: New Connection

  3. Fill in:

    • Host: localhost

    • Port: 5432

    • Username: myuser

    • Password: mypassword

    • Database: mydb


πŸ–₯️ On Windows

1. Install PostgreSQL

2. Add PostgreSQL to System PATH So you can use psql in CMD/PowerShell:

  • Add: C:\Program Files\PostgreSQL\15\bin (adjust based on version)

3. Create a New Database

Open pgAdmin or PowerShell:

4. Install VS Code PostgreSQL Extension

Same as macOS:

  • Open VS Code β†’ Extensions β†’ Search PostgreSQL by Microsoft β†’ Install

5. Connect from VS Code

Use Ctrl+Shift+P β†’ PostgreSQL: New Connection


βš™οΈ Run SQL Files in VS Code

  1. Create a new file: init.sql

  2. Write SQL statements (e.g., CREATE TABLE, INSERT INTO)

  3. Right-click in the editor β†’ Execute Query


βœ… Optional: Load Your .sql File

From terminal or command prompt:


Tool

Use

pgAdmin

GUI for PostgreSQL

DBeaver

Cross-platform DB GUI

Postico (Mac)

Native PostgreSQL GUI

psql

Command-line PostgreSQL tool


πŸ’‘Create Employee DB like schema below:

πŸ“Create employee_db

πŸ‘‰Inside the psql prompt, run:

πŸ‘‰Create Table (SQL schema):

πŸ‘‰Insert DB:


🧠 Using MCP DB Server to working with DB

Last updated