How to Run pgAdmin 4 in WSL and Access from Windows| Sabbirz | Blog

How to Run pgAdmin 4 in WSL and Access from Windows

pgAdmin 4 in WSL and Access from Windows

Setting up pgAdmin 4 on WSL & Access from Windows

Stop installing heavy desktop apps for database management! If you're a developer rocking WSL (Windows Subsystem for Linux), there is a much smarter, lightweight, and "pro" way to handle your PostgreSQL databases.

Why clutter your Windows registry when you can run the pgAdmin 4 web interface cleanly inside Linux and access it directly from your favorite Windows browser? It feels native, saves resources, and makes you look like a total tech wizard. πŸ§™β€β™‚οΈβœ¨

Ready to level up your workflow? Let's dive in! πŸ‘‡


πŸ› οΈ Step 1: Install pgAdmin 4 (Web Version)

First things first. We don't need the bloat of the desktop client. We are going for the sleek, efficient web version.

1️⃣ Add the Repository Key

Open your WSL terminal and run this command to fetch the official key. This ensures we're getting the genuine package. πŸ”

curl -fsS https://www.pgadmin.org/static/packages_pgadmin_org.pub | sudo gpg --dearmor -o /usr/share/keyrings/packages-pgadmin-org.gpg

2️⃣ Add the Repository

Now, tell your system where to find the pgAdmin goodies by copying and pasting this magic line:

sudo sh -c 'echo "deb [signed-by=/usr/share/keyrings/packages-pgadmin-org.gpg] https://ftp.postgresql.org/pub/pgadmin/pgadmin4/apt/$(lsb_release -cs) pgadmin4 main" > /etc/apt/sources.list.d/pgadmin4.list'

3️⃣ Update & Install

Refresh your package list and install the pgadmin4-web package. Run:

sudo apt update
sudo apt install pgadmin4-web

βš™οΈ Step 2: Configure the Web Server

Now for the fun part! We need to set up a lightweight web server (Apache is used by default here) to serve the pgAdmin app. Think of it like setting up phpMyAdmin, but for Postgres. 🐘

Run the built-in setup script:

sudo /usr/pgadmin4/bin/setup-web.sh

πŸ“ You'll need to answer a few quick prompts:

  1. Email: Enter your email address. (This will be your username for logging into the pgAdmin interface). πŸ“§
  2. Password: Create a strong password. (Don't forget it! πŸ”‘).
  3. Store the email id and password
  4. Restart Apache: When asked to restart the web server, type y (Yes) and hit Enter. βœ…

🌐 Step 3: Access from Windows

Here is the magic of WSL 2. It automatically forwards ports to your Windows host. No complex networking configuration required! πŸŽ‰

  1. Open your favorite browser on Windows (Chrome, Edge, Firefox, Arcβ€”whatever floats your boat πŸ›Ά).
  2. Navigate to: πŸ‘‰ http://localhost/pgadmin4
  3. Login using the email and password you just created in Step 2.

Boom! You are now inside the pgAdmin dashboard. But wait... where are your databases? πŸ€”

Unlike phpMyAdmin, pgAdmin doesn't just guess where your database server is. We need to introduce them properly.


πŸ”Œ Step 4: Connect Your Database (The Final Piece)

Let's register your PostgreSQL server so pgAdmin can talk to it.

1️⃣ Add the Server

On the pgAdmin dashboard (left sidebar), Right-click on Servers πŸ‘‰ Register πŸ‘‰ Server...

2️⃣ Configure the Connection

A popup window will appear. Fill it out with these details:

General Tab

  • Name: Give it a friendly name like Ubuntu DB or Localhost. (This is just a label for you).

Connection Tab

  • Host name/address: 127.0.0.1

    ⚠️ Important: Use 127.0.0.1 instead of localhost. Sometimes inside Linux containers/WSL, "localhost" tries to use a socket file which can cause permission headaches. The IP address forces a TCP connection. 🧠

  • Port: 5432 (Default)
  • Maintenance database: [postgresDBname]

    Note: By default, this is postgres, but if you created a specific database, it’s often safer to connect directly to that.

  • Username: root (Or your specific postgres username)
  • Password: Enter the Postgres user password you created when setting up the database (Not the pgAdmin password from Step 2!).
  • Save password?: Toggle this to Yes so you don't have to type it every time.

Click Save. πŸ’Ύ

If everything is correct, your server will appear, and you can browse your tables! πŸ₯‚


🚨 Troubleshooting: "Peer authentication failed"?

Did you get a scary red error saying FATAL: Peer authentication failed for user 'sab'? 😱

Don't panic! This just means PostgreSQL is being a bit too secure. It's configured to trust the operating system user identity rather than the password you just typed. We need to tell it to accept password logins.

πŸ”§ The Fix

  1. Go back to your Ubuntu/WSL terminal.
  2. Edit the authentication config file:
    sudo nano /etc/postgresql/16/main/pg_hba.conf
    (Note: Replace 16 with 14 or 15 if you are using an older version).
  3. Find the line that looks like this:
    local   all             all                                     peer
  4. Change peer to md5 (or scram-sha-256):
    local   all             all                                     scram-sha-256
    Also, ensure the IPv4 line says scram-sha-256 or md5:
    host    all             all             127.0.0.1/32            scram-sha-256
  5. Save & Exit: Press Ctrl+O then Enter to save, and Ctrl+X to exit.
  6. Restart Postgres to apply the changes:
    sudo service postgresql restart

Now try clicking Save in pgAdmin again. It should work perfectly! πŸŽ‰

Happy Coding! πŸ’»βœ¨

Related posts