For Unix environnement the command line is

psql -U USERNAME -h localhost dbname

For a Windows environment, you may consider replacing "-" with "/"

-U option able you to choose a user to connect with

-h option able you to connect with the TCPIP protocol, you may consider it useless for Windows

Answer from Charkan on Stack Overflow
๐ŸŒ
w3resource
w3resource.com โ€บ PostgreSQL โ€บ connect-to-postgresql-database.php
Connect to PostgreSQL Database on Linux, Windows | w3resource
April 1, 2026 - At the command line in your operating system, type the following command. ... user@user-pc:~$ sudo -i -u postgres postgres@user-pc:~$ psql psql (9.3.5, server 9.3.6) Type "help" for help.
๐ŸŒ
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 - 2. Enter the psql command and specify ... called phoenixnap, using the postgres user, enter: ... If the database is password-protected, psql prompts for the password....
Discussions

cmd - How to connect to PostgreSQL through CLI? - Stack Overflow
I am trying to access postgresql through the command line. However, whenever it is time for me to enter my password, I get the following error: Fatal: password authentication failed for user RMehta... More on stackoverflow.com
๐ŸŒ stackoverflow.com
postgresql - How do I specify a password to 'psql' non-interactively? - Stack Overflow
I am trying to automate database creation process with a shell script and one thing I've hit a road block with passing a password to psql. Here is a bit of code from the shell script: psql -U $DB_... More on stackoverflow.com
๐ŸŒ stackoverflow.com
connect to postgres database command line linux with password
Using an existing graphical frontend ... a database. Provides information on Asterisk, an open source telephony application. Backup the pg_hba.conf file by copying it to a different location or just rename it to pg_hba.conf.bk. We can connect to PostgreSQL simply using a command line interface in Linux operating system distribution. I tried the -w option explained in this link to not ask for password ... More on chopstickhousegr.com
๐ŸŒ chopstickhousegr.com
January 14, 2021
Access postgresql server on Linux command line, but not GUI (beekeeper)
sudo is a program to allow you to run a command as another user. Your user account needs to be named in the sudoers file or in a group that is listed there to use sudo. sudo -i -u postgres logs you in as the system user postgres. Anything you run after that is run as the user you used sudo to log in as. sudo -i -u postgres psql logs you in as the system user postgres, then runs the command psql. I just ran it on my Debian system and it logged me in and got me straight into the database. If you can do that, you can then use ALTER USER to change the password to something you know. More on reddit.com
๐ŸŒ r/PostgreSQL
4
0
August 9, 2023
๐ŸŒ
EnterpriseDB
enterprisedb.com โ€บ postgres-tutorials โ€บ connecting-postgresql-using-psql-and-pgadmin
Connecting PostgreSQL using psql and pgAdmin | EDB
On Linux, you will explicitly need to pass these parameters at the shell: For PostgreSQL: -bash-4.2$ psql -d postgres -U postgres ยท For Advanced Server: -bash-4.2$ psql -d edb -U enterprisedb ยท If connecting to a remote server from any of ...
๐ŸŒ
UbuntuMint
ubuntumint.com โ€บ home โ€บ postgresql โ€บ how to connect to postgres database from command line
How to Connect to Postgres Database From Command Line
December 4, 2023 - If your server is listening on ... port number it is listening on). Press Enter after entering the command, and provide the password to establish a connection to the specified database....
๐ŸŒ
Alvin Alexander
alvinalexander.com โ€บ blog โ€บ post โ€บ postgresql โ€บ log-in-postgresql-database
Postgres login: How to log into a Postgresql database | alvinalexander.com
January 31, 2026 - Usage: psql [OPTIONS]... [DBNAME [USERNAME]] General options: -d DBNAME specify database name to connect to (default: "root") -c COMMAND run only single command (SQL or internal) and exit -f FILENAME execute commands from file, then exit -l list available databases, then exit -v NAME=VALUE set psql variable NAME to VALUE -X do not read startup file (~/.psqlrc) --help show this help, then exit --version output version information, then exit Input and output options: -a echo all input from script -e echo commands sent to server -E display queries that internal commands generate -q run quietly (n
Find elsewhere
๐ŸŒ
Chopstickhousegr
chopstickhousegr.com โ€บ forum โ€บ p5lf5 โ€บ article.php
connect to postgres database command line linux with password
January 14, 2021 - Found inside รขย€ย“ Page 19On a ... to use the host connection type. You can add this command line at the begining of your script: set PGPASSWORD=[your password] su - postgres รขย€ยฆ then attempt a connection to PostgreSQL....
๐ŸŒ
Medium
medium.com โ€บ @linglijunmail โ€บ automating-postgresql-database-connections-in-scripts-a-guide-to-password-management-fc5e4558b012
Automating PostgreSQL Database Connections in Scripts: A Guide to Password Management | by Jason Anderson | Medium
February 23, 2024 - Note, for security reasons, it is recommended not to include the password directly in the command line but to use the -W parameter to let psql prompt you for the password, or manage the password through a configuration file. In a script that executes in a loop and connects to the PostgreSQL database, itโ€™s indeed inconvenient to manually enter the password each time.
๐ŸŒ
Reddit
reddit.com โ€บ r/postgresql โ€บ access postgresql server on linux command line, but not gui (beekeeper)
r/PostgreSQL on Reddit: Access postgresql server on Linux command line, but not GUI (beekeeper)
August 9, 2023 -

I don't expect anyone to be familiar with beekeeper, however, I'm hoping that maybe there are some common threads to this issue, via other GUIs as well.

I use this command to get into the postgres server as user postgres ( I think ) on command line:

sudo -i -u postgres psql

I'm prompted for my user password (one I use to get into computer), however, I'm not prompted for any other password, like one specifically for the database.

when I use the same info for my GUI (beekeeper studio), I get this:

connect ECONNREFUSED 127.0.0.1:5432

all i need is to access it myself on the localhost. Oddly I''m able to access the postgres database as user postgres on the same GUI, on my Apple running OS 11. I do require a password for that database, that I set myself, but it was so long ago,that I don't remember how or why I created a password for the default postgres db.

It might help to explain that I may be an unconventional poster here, since I'm not a dba nor do I have dba knowledge. I used to be just another Microsoft SQL admin user, at my last couple jobs, running basic queries and whatnot. The type of employee who had to submit a ticket to a dba, to update a table for any reason. I found that I preferred SQL for organizational tasks, that most consumers use spreadsheet applications for, so I found postgresql and the GUI Beekeeper, to basically recreate my Microsoft SQL admin experience, on my Apple OS ( and now Linux ) at home. I expect I need to provide more information, please let me know, thank you

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ manage-postgresql-with-psql
How to Manage PostgreSQL Databases from the Command Line with psql
October 3, 2025 - When you installed PostgreSQL, a default database and user were created, both called postgres. So enter psql -d postgres -U postgres to connect to the postgres database as the postgres superuser. ... You will be prompted for a password.
๐ŸŒ
PostgreSQL
postgresql.org โ€บ docs โ€บ 9.1 โ€บ app-psql.html
PostgreSQL: Documentation: 9.1: psql
October 27, 2016 - Show help about psql command line arguments, and exit. psql returns 0 to the shell if it finished normally, 1 if a fatal error of its own occurs (e.g. out of memory, file not found), 2 if the connection to the server went bad and the session was not interactive, and 3 if an error occurred in a script and the variable ON_ERROR_STOP was set. psql is a regular PostgreSQL client application. In order to connect to a database you need to know the name of your target database, the host name and port number of the server, and what user name you want to connect as.
๐ŸŒ
Benburwell
benburwell.com โ€บ posts โ€บ how-i-connect-to-postgres-databases
How I Connect to Postgres Databases - Ben Burwell
December 13, 2022 - Throughout this page, I'll pretend ... can use a connection URL, CLI flags, or you can use a series of libpq options: $ psql 'host=db1.internal.net user=app dbname=db password=sesame port=5432'...
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ postgres โ€บ psql password
How to Connect to PostgreSQL With Password | Delft Stack
February 2, 2024 - One simple method is to type psql in the command line, and it will ask you the password for the admin user. If you type only psql without mentioning the username in the command, the system will ask you the admin user password for the PostgreSQL, ...
๐ŸŒ
Tutorial Teacher
tutorialsteacher.com โ€บ postgresql โ€บ connect-to-postgresql-database
Connect to PostgreSQL Database using SQL Shell & pgAdmin
SQL Shell can be opened from the command prompt. Open a command prompt and navigate to your local C:\Program Files\PostgreSQL\14\bin and enter the following command. (you may enter this path to your environment variable) ...
๐ŸŒ
KB Hosting
kb.hosting.com โ€บ docs โ€บ connect-to-postgresql-from-the-command-line
Connecting to PostgreSQL using the command line - hosting.com
To connect to PostgreSQL from the command line, follow these steps: Log in to your account using SSH. At the command line, type the following command. Replace dbname with the name of the database, and username with the database username: ... ...
๐ŸŒ
A2 Hosting
a2hosting.com โ€บ kb โ€บ developer-corner โ€บ postgresql โ€บ connect-to-postgresql-from-the-command-line
The Best Web Hosting Services at 20x Speeds | hosting.com
Get the best hosting with premium hardware for speed and reliability. 24/7/365 global support. Try risk-free with our money back guarantee.
๐ŸŒ
Yolinux
yolinux.com โ€บ TUTORIALS โ€บ LinuxTutorialPostgreSQL.html
YoLinux Tutorial: The PostgreSQL Database and Linux
Command line: [prompt]$ createdb bedrock -U user-name -W You will be prompted for a password. (or execute as Linux user postgres without -U and -W options) ... Restore: [prompt]$ psql -e template1 < /var/lib/pgsql/backup/db.bak The table template1 is the default administrative database. postgres=# ...
๐ŸŒ
Linux Tip
linuxscrew.com โ€บ home โ€บ programming โ€บ databases โ€บ how to connect to postgresql from the linux command line
How to Connect to PostgreSQL from the Linux Command Line
July 24, 2023 - psql -h my_database_server_hsot -p 5432 -d database_name ... Unless you are logged in as the user postgres and connecting to localhost, you will most likely need to specify the username (-U) and ask to be prompted for a password (-W) to connect:
๐ŸŒ
NTU Singapore
www3.ntu.edu.sg โ€บ home โ€บ ehchua โ€บ programming โ€บ sql โ€บ PostgreSQL_GetStarted.html
Getting Started with PostgreSQL
By running the "psql" command as a UNIX user which is also configured as PostgreSQL user using so-called IDENT/PEER authentication, e.g., "sudo -u postgres psql". Via TCP/IP connection using PostgreSQL's own managed username/password (using so-called MD5 authentication).