Accessing PostgreSQL Database in WSL from pgAdmin on Windows

Abstract: Resolve pgAdmin connection failures to PostgreSQL in WSL by configuring listen addresses, authentication rules, and Windows firewall inbound ports.

Created: 2026-09-11 by
You can connect pgAdmin running on Windows to a PostgreSQL instance inside WSL by configuring network accessibility and authentication. This setup requires exposing the WSL database port to the Windows host, updating the pg_hba.conf file to allow remote connections, and ensuring your Windows firewall permits the traffic. By following these steps, you establish a secure bridge between the two environments without needing to log into the Linux subsystem directly.

Understanding the Network Boundary

By default, services running inside WSL are isolated from the Windows host. While WSL 2 uses a virtual switch to manage networking, the PostgreSQL server listening on localhost within the Ubuntu distro is not automatically reachable from Windows applications like pgAdmin. To fix this, you must treat the WSL instance as a remote server rather than a local one.

The primary challenge is that the IP address of your WSL distribution changes every time you restart it or reboot your computer. Therefore, hardcoding an IP address in your connection settings is unreliable. Instead, you should use the hostname localhost from the Windows perspective, which routes traffic through the NAT interface provided by Hyper-V or the WSL virtual switch.

Configuring PostgreSQL for Remote Access

Before attempting any network configuration, you must ensure the PostgreSQL service inside WSL accepts connections from outside its immediate environment. This involves editing two critical configuration files: postgresql.conf and pg_hba.conf.

Modifying postgresql.conf

First, open your terminal in WSL and locate the configuration directory. On most modern distributions, this is found in /etc/postgresql/[version]/main/. Open the postgresql.conf file using a text editor like nano or vim.

sudo nano /etc/postgresql/*/main/postgresql.conf

Search for the listen_addresses directive. By default, it is often commented out or set to 'localhost'. Change this value to '*' to listen on all available network interfaces. This allows the database to accept connections from the Windows host via the loopback adapter.

listen_addresses = '*'

Updating pg_hba.conf for Authentication

Next, you must configure who is allowed to connect. Open the pg_hba.conf file in the same directory.

sudo nano /etc/postgresql/*/main/pg_hba.conf

Add a line at the end of the file to permit IPv4 local connections. It is crucial to use a secure authentication method such as scram-sha-256 or md5 (if legacy compatibility is required, though scram is preferred). Avoid using trust unless you are in a strictly isolated development environment, as this bypasses password verification entirely.

host    all             all             127.0.0.1/32            scram-sha-256

If you want to allow connections from other devices on your local network, you can replace 127.0.0.1/32 with 0.0.0.0/0, but restrict this only if necessary. After saving both files, restart the PostgreSQL service inside WSL to apply the changes:

sudo service postgresql restart

Handling Windows Firewall Rules

Even with correct PostgreSQL configuration, Windows Defender Firewall may block incoming connections on the port PostgreSQL uses (default 5432). You need to create an inbound rule to allow this traffic.

  1. Open the Windows Security app and navigate to Firewall & network protection.
  2. Select Advanced settings to open the Windows Defender Firewall with Advanced Security console.
  3. Right-click on Inbound Rules and select New Rule....
  4. Choose Port and click Next.
  5. Select TCP and enter 5432 in the Specific local ports field.
  6. Allow the connection and apply it to Domain, Private, and Public profiles.
  7. Name the rule something identifiable, such as WSL PostgreSQL Access.

This step ensures that the packet traveling from pgAdmin on Windows reaches the WSL virtual switch without being dropped by the host OS.

Connecting via pgAdmin

With the backend configured and the firewall open, you can now add the server in pgAdmin on Windows.

  • Open pgAdmin and right-click on Servers in the browser tree.
  • Select Create > Server.
  • In the General tab, give the server a name, such as WSL Postgres.
  • Navigate to the Connection tab.
  • Set the Host name/address to localhost. This is the key trick; from Windows, localhost points to the WSL virtual adapter.
  • Set the Port to 5432.
  • Enter the Maintenance database (usually postgres) and your username/password.

If the connection fails, verify that the WSL instance is actually running by executing wsl in PowerShell. If the IP address has changed, localhost still works because the NAT mapping handles the translation dynamically. For more details on managing WSL connectivity, see Troubleshooting SSH WSL Machine: Can't Connect to Localhost.

Troubleshooting Common Issues

If you cannot connect, first check if PostgreSQL is listening on the correct address inside WSL by running netstat -tuln | grep 5432. Ensure the output shows 0.0.0.0:5432 or *:5432, not just 127.0.0.1:5432. Additionally, verify that your Windows firewall rule is active and that no third-party antivirus software is blocking the connection. If you are moving data between systems, you might also find it useful to Mounting Windows Folder in WSL (Debian) using fstab for easier file access during debugging.

Frequently Asked Questions

Why does my WSL IP address keep changing?

WSL 2 runs inside a lightweight utility VM managed by Hyper-V, which reassigns dynamic IP addresses upon restart. This is why you should always connect using localhost from Windows rather than a static IP, as the NAT layer handles the routing transparently.

Is it safe to use md5 authentication?

While md5 is widely supported, it is considered weaker than scram-sha-256. If your PostgreSQL version supports it, prefer scram-sha-256 for better security against password sniffing attacks. Only use md5 if you have older clients that do not support SCRAM.

Can I connect to WSL PostgreSQL from another computer on my network?

Yes, but you must change the listen_addresses in postgresql.conf and update pg_hba.conf to allow connections from your subnet (e.g., 192.168.1.0/24). You will also need to create additional firewall rules on Windows to allow inbound traffic from those external IPs.

What if pgAdmin says "Connection refused"?

This usually means PostgreSQL is not running inside WSL or is not listening on the expected port. Verify the service status with sudo service postgresql status and ensure you have restarted the service after modifying postgresql.conf to apply the new listen addresses.

Do I need to install PostgreSQL inside WSL?

No, you can use the native Windows installer for PostgreSQL and connect to it from WSL, or vice versa. However, keeping the database inside WSL is common for developers working primarily in Linux environments who want to maintain parity with production servers.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More