To connect the PostgreSQL via Command line you have to install a PostgreSQL provided tool called "psql" if not installed already.

Then use the below command to connect to PostgreSQL database.

psql -h localhost -p 5432 -U postgres 

Explanation:

localhost: is the hostname of machine where PostgreSQL is installed.
5432 : is the default PostgreSQL port
postgres : is the username of to connect to DB

Solution for your issue:

FATAL: password authentication failed for user "testuser"

As error message specified, either user not exists on database or the password that you supplied to connect is incorrect. Re-verify your user and password, and try again.

All the best.

Answer from user8406805 on Stack Overflow
🌐
Neon
neon.com › postgresql › getting-started › connect-to-postgresql-database
Connecting to PostgreSQL Database Server
You will learn how to connect to the PostgreSQL Database Server via client tools including psql and pgAdmin.
🌐
PhoenixNAP
phoenixnap.com › home › kb › databases › how to connect to a postgresql database from linux or windows
How to Connect to a PostgreSQL Database from Linux or Windows
December 12, 2025 - To connect to a PostgreSQL database from the Windows Command Prompt (CMD): 1. Type Command Prompt in the Windows search bar and launch the app.
🌐
Micro Focus
microfocus.com › documentation › idol › IDOL_12_0 › MediaServer › Guides › html › English › Content › Getting_Started › Configure › _TRN_Set_up_PostgreSQL.htm
Set Up a PostgreSQL Database on Windows - Micro Focus
In the PostgreSQL Unicode ODBC Driver (psqlODBC) Setup dialog box, click Save to close the dialog box. In the ODBC Data Source Administrator dialog box, click OK to close the dialog box. You can now configure Media Server to connect to the database (see Configure Media Server).
🌐
PostgreSQL
postgresql.org › docs › current › tutorial-accessdb.html
PostgreSQL: Documentation: 18: 1.4. Accessing a Database
May 14, 2026 - Once you have created a database, you can access it by: Running the PostgreSQL interactive terminal program, called psql, which allows you to interactively enter, edit, and execute SQL commands.
🌐
DevArt
blog.devart.com › home › how to › connect to postgresql database using psql, pgadmin, and postgresql client tool
PostgreSQL PSQL connect to database - 3 best methods: psql, pgAdmin, and dbForge Studio
July 23, 2025 - Learn how to connect to your PostgreSQL database using psql, pgAdmin, and dbForge Studio. Step-by-step instructions for setting up local and remote connections, fixing common errors, and more.
🌐
EnterpriseDB
enterprisedb.com › postgres-tutorials › connecting-postgresql-using-psql-and-pgadmin
Connecting PostgreSQL using psql and pgAdmin | EDB
On Windows, you can find psql in the Program Files, and you should be able to launch it in a command prompt simply by clicking on it. On a Mac you should be able to locate psql under the Applications > PostgreSQL (version number) > SQL Shell (psql).
Find elsewhere
🌐
Tutorial Teacher
tutorialsteacher.com › postgresql › connect-to-postgresql-database
Connect to PostgreSQL Database using SQL Shell & pgAdmin
You can use it to create, alter, ... postgres database using SQL Shell (psql). On Windows, press Windows keys -> All apps -> PostgreSQL 14 -> click on SQL Shell (psql), as shown below....
🌐
Medium
medium.com › @zum.hatice › how-to-create-a-postgresql-db-and-connect-in-windows-b26eaa48c7fb
How to Create a Postgresql DB and Connect in Windows | by Hatice Zümrüt | Medium
February 27, 2024 - You may close Postgresql in cmd by ... To connect to your DB, you may use DBeaver. After downloading and launching the application, you will have the below window. As you can see there is a icon in upper-left.
🌐
CommandPrompt Inc.
commandprompt.com › education › how-to-connect-to-postgres-database-server
How to Connect to Postgres Database Server — CommandPrompt Inc.
March 2, 2023 - Clicking on the “pgAdmin” app will lead you to the following window: Provide the superuser password and hit the “OK” button to connect to the Postgres Database Server. Open the Query tool and use any command/query to confirm if the ...
Address   2950 Newmarket ST STE 101 - 231, 98226, Bellingham
🌐
Database Tutorials
dbtut.com › home › postgresql
Connect To Postgres from command line in Windows - Database Tutorials
October 6, 2020 - We will connect to postgresql from command line in windows. Open windows command line and then go to the path where psql is locate.
🌐
W3Schools
w3schools.com › postgresql › postgresql_getstarted.php
PostgreSQL - Get Started
The suggested username is [postgres], which is correct, at least for me, press [Enter] to accept: Enter the password you chose when you installed the PostgreSQL database, my password is 12345678: The result might look like an error, but if it shows psql (15.2) or any other version, and in the end you see the postgres=# command (and maybe a warning in between), then you have successfully connected to the database!
🌐
NetIQ
netiq.com › documentation › identity-manager-47 › setup_windows › data › connecting-to-a-remote-postgresql-database.html
Connecting to a Remote PostgreSQL Database - NetIQ Identity Manager Setup Guide for Windows
To allow a remoteTCP/IP connection, add the following entry to the C:\NetIQ\idm\postgres\data\postgresql.conf file: ... If you have multiple interfaces on the server, you can specify a specific interface to be listened. Add a client authentication entry to the pg_hba.conf file. By default, PostgreSQL accepts connections only from the localhost.
🌐
w3resource
w3resource.com › PostgreSQL › connect-to-postgresql-database.php
Connect to PostgreSQL Database on Linux, Windows | w3resource
April 1, 2026 - Connect to PostgreSQL Database on Linux, Windows: This tutorial shows you various ways to connect to PostgreSQL database on different platform by using interactive terminal program called psql and pgAdmin GUI application.
🌐
Mytechmantra
mytechmantra.com › home › postgresql › install postgresql on windows: step‑by‑step beginner’s guide
Install PostgreSQL on Windows (Step by Step Beginner’s Guide) - MyTechMantra.com
January 13, 2026 - Learn step by step how to set Windows PATH for Postgres tools. Easily add PostgreSQL to PATH Windows 11, configure PostgreSQL environment variables Windows, and run Postgres from the command line. Connect to PostgreSQL Database Server Using psql and pgAdmin
Top answer
1 of 9
132

WSL2 assigns IP address to the Windows host dynamically and the IP addresses can change without even rebooting Windows (see Notes below). So to reliably connect we'll need to:

  1. Allow Windows and Postgres to accept connections from the WSL2 IP address range (not allowed by default)
  2. From WSL2, determine the Windows/Postgresql host's IP address (which is dynamic) when connecting via psql. We'll make this convenient via .bashrc and alias.

Unfortunately I couldn't find the exact specification for the WSL2 IP address range. From several tests/reboots it appears that WSL2 is assigning IP addresses primarily in range of 172.*.*.* but I have occasionally been assigned 192.*.*.* so we'll use these when configuring the firewall and Postgres.

Add Windows Firewall Inbound Port Rule for WSL2 IP Addresses:

  1. Open Windows Defender Firewall with Advanced Security
  2. Click New Rule...
  3. Select Port for rule type
  4. Select TCP and for Specific local ports enter 5432
  5. Select Allow the connection. Connecting from WSL2 won't be secure so don't select the secure option
  6. Select at least Public. Can select Domain and Private as well. I could only connect if Public was selected
  7. Name the rule e.g. Postgres - connect from WSL2 and create it
  8. Right click newly created rule and select Properties then click on the Scope tab
  9. Under Remote IP address, select These IP addresses then click Add... and enter range 172.0.0.1 to 172.254.254.254
  10. Repeat step 9 for IP address range 192.0.0.1 to 192.254.254.254
  11. Click Apply then OK
  12. Make sure rule is enabled

Configure Postgres to Accept Connections from WSL2 IP Addresses

Assuming a default install/setup of Postgresql for Windows the following files are located under C:\Program Files\PostgreSQL\$VERSION\data

Verify that postgresql.conf has following set:

listen_addresses = '*'

This should already be set to '*' so nothing do here.

Update pg_hba.conf to allow connections from WSL2 range e.g. for Postgresl 12:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
host    all             all             172.0.0.0/8             md5
host    all             all             192.0.0.0/8             md5

For Postgresql 13+ you should use scram-sha-256 as the method.

Restart Postgres for changes to take effect. This can be done either from the Windows Services app or from cmd with Administrator privileges e.g. for Postgresql 12:

net stop postgresql-x64-12
net start postgresql-x64-12

WSL Shell Conveniences

In WSL, add following to your ~/.bashrc or similar:

# Add DNS entry for Windows host
if ! $(cat /etc/hosts | grep -q 'winhost'); then
  echo 'Adding DNS entry for Windows host in /etc/hosts'
  echo '\n# Windows host - added via ~/.bashhrc' | sudo tee -a /etc/hosts
  echo -e "$(grep nameserver /etc/resolv.conf | awk '{print $2, "   winhost"}')" | sudo tee -a /etc/hosts
fi

Then reload your .bashrc changes: source ~/.bashrc

Usage

psql -h winhost -p 5432 -U postgres

Notes:

  • The IP address assigned to the Windows host by WSL2 is not the same as the IP address assigned to your physical Windows machine on your network. WSL2 uses vEthernet connections.
  • You can inspect the vEthernet connections via Control Panel\Network and Internet\Network Connections
  • Note that when looking at the IPv4 properties that the IP addresses will appear as if they are statically set but they aren't! Try rebooting and inspecting IPv4 properties again
  • If one day you're unable to connect to Postgres, check that winhost is in the IP address range per firewall rules. Could be WSL has assigned an IP address that we weren't expecting!
2 of 9
24

In WSL2 you need to use host IP to connect

to get host IP

grep nameserver /etc/resolv.conf | awk '{print $2}'

then you need to allow TCP 5432 inbound Rules in 'Windows Defender Firewall with Advanced Security'


I made my self PS.you still need to allow TCP 5432 in Firewall

put this in ~/.bashrc

cat /etc/hosts | grep 172.; test $? -eq 0 && $1 || echo -e "$(grep nameserver /etc/resolv.conf | awk '{print $2, " host"}')\n$(cat /etc/hosts)" | sudo tee /etc/hosts

its append host IP to /etc/hosts if not exist before(usually happened when restart wsl or computer)

then

psql -h host -p 5432 -U postgres
🌐
The Windows Club
thewindowsclub.com › the windows club › downloads › install postgresql on windows: step by step guide
Install PostgreSQL on Windows: Step by Step guide
January 30, 2026 - Finally, we are going to connect to your PostgreSQL using PgAdmin. So, search out “PgAdmin” from the Start Menu and open the application. To check your database, go to Servers, expand it, enter your password when asked, and then access your DB.