There is no default username and password without you creating one. The simplest possible setup is to follow these steps to set up your own user as a superuser.

At a terminal prompt, create a postgres user with your own username

sudo -u postgres createuser --superuser $USER

Start the postgresql command prompt as your username but running as root since you didn't set a password yet;

sudo -u postgres psql

At the postgresql prompt, set your password;

\password $USER

After that, you should be able to log on just fine.

The setup is more thoroughly documented here.

EDIT:

If you get stuck not being able to authenticate automatically as the postgres user, you may want to compare your /etc/postgresql/9.1/main/pg_hba.conf (ie authentication config file) with the following lines from mine that works; you can get the uncommented ones using

grep -v ^# pg_hba.conf

The "local" lines should be the essential ones in this case since you can't authenticate even from the same machine;

local   all             postgres                                peer
local   all             all                                     peer
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
Answer from Joachim Isaksson on Stack Overflow
🌐
NTU Singapore
www3.ntu.edu.sg › home › ehchua › programming › sql › PostgreSQL_GetStarted.html
Getting Started with PostgreSQL
Restart the server, and login via sudo -u postgres sql. The system will prompt you for the password. Using user "postgres" (which is a UNIX user as well as PostgreSQL user) to run "psql" involves switching user (via "sudo -u username" or "su - username").
Top answer
1 of 3
54

There is no default username and password without you creating one. The simplest possible setup is to follow these steps to set up your own user as a superuser.

At a terminal prompt, create a postgres user with your own username

sudo -u postgres createuser --superuser $USER

Start the postgresql command prompt as your username but running as root since you didn't set a password yet;

sudo -u postgres psql

At the postgresql prompt, set your password;

\password $USER

After that, you should be able to log on just fine.

The setup is more thoroughly documented here.

EDIT:

If you get stuck not being able to authenticate automatically as the postgres user, you may want to compare your /etc/postgresql/9.1/main/pg_hba.conf (ie authentication config file) with the following lines from mine that works; you can get the uncommented ones using

grep -v ^# pg_hba.conf

The "local" lines should be the essential ones in this case since you can't authenticate even from the same machine;

local   all             postgres                                peer
local   all             all                                     peer
host    all             all             127.0.0.1/32            md5
host    all             all             ::1/128                 md5
2 of 3
5

During the installation process you've probably missed steps:

Now we need to reset the password for the ‘postgres’ admin account for the server, so we can use this for all of the system administration tasks. Type the following at the command-line (substitute in the password you want to use for your administrator account):

sudo su postgres -c psql template1
template1=# ALTER USER postgres WITH PASSWORD 'password';
template1=# \q

That alters the password for within the database, now we need to do the same for the unix user ‘postgres’:

sudo passwd -d postgres
sudo su postgres -c passwd

Now enter the same password that you used previously.

http://hocuspokus.net/2008/05/install-postgresql-on-ubuntu-804/

Discussions

database - How can I log in and authenticate to PostgreSQL after a fresh install? - Stack Overflow
I did a new install of PostgreSQL 8.4 on Linux Mint/Ubuntu. How do I create a user for PostgreSQL and log in using psql? When I type psql, it just tells me psql: FATAL: Ident authentication failed... More on stackoverflow.com
🌐 stackoverflow.com
ubuntu - Logging into PostgreSQL on Linux - Stack Overflow
I have installed Postgre SQL on my local Ubuntu 20.04 as described here: https://computingforgeeks.com/how-to-install-postgresql-13-on-ubuntu/ In order to log in to Postge SQL I have to switch to u... More on stackoverflow.com
🌐 stackoverflow.com
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
linux - Postgres Initial Setup and User Login - Database Administrators Stack Exchange
Full disclosure, new to postgres and new to linux. I am having trouble understanding the user management side of postgresql. I installed on a Ubuntu 19.04 based os using sudo apt update sudo apt More on dba.stackexchange.com
🌐 dba.stackexchange.com
🌐
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 - This is psql 8.0.3, the PostgreSQL interactive terminal. 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
🌐
Kompulsa
kompulsa.com › home › postgresql tutorials: how to log into postgresql in ubuntu
PostgreSQL Tutorials: How To Log Into PostgreSQL In Ubuntu
September 16, 2025 - A password will be e-mailed to you. ... If you’re looking for the default password for PostgreSQL on Ubuntu, there is none. To log into PostgreSQL via the command line on Ubuntu, you have to switch to the user ‘postgres’ using a privileged user’s account.
🌐
Silverspringenergy
silverspringenergy.com › logging-in-to-postgresql-database-on-linux-terminal
Logging in to PostgreSQL database on Linux terminal – Silver Spring Energy Consulting Ltd.
April 11, 2019 - This section assumes command line access to the PostgreSQL server from a terminal window. The server used in this section is running Ubuntu Linux 18.10.
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-install-and-use-postgresql-on-ubuntu-20-04
How To Install and Use PostgreSQL on Ubuntu | DigitalOcean
November 6, 2025 - Once at the PostgreSQL prompt, you can use CREATE ROLE and CREATE DATABASE to set up your new user and database. Create a New Role (User): While the createuser shell command is interactive, the CREATE ROLE command lets you define everything in one statement. To create a user named sammy that can log in (LOGIN) and has a password, run:
🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-install-and-use-postgresql-on-ubuntu-16-04
How To Install and Use PostgreSQL on Ubuntu 16.04 | DigitalOcean
February 18, 2026 - When I attempt to log in to the new database with ident based authentication ('sudo -i -u sammy psql) under the new role, it prompts me for a postgres password which we never created. However, if I simply type ‘psql’, it logs me in successfully under the new role. Speaking of passwords, should we create a password for the new database? I’m using an Ubuntu server which has to be ssh’d into.
Find elsewhere
Top answer
1 of 7
239

There are two methods you can use. Both require creating a user and a database.

By default psql connects to the database with the same name as the user. So there is a convention to make that the "user's database". And there isn't any reason to break that convention if your user only needs one database. We'll be using mydatabase as the example database name.

  1. Using createuser and createdb, we can be explicit about the database name,

    $ sudo -u postgres createuser -s $USER
    $ createdb mydatabase
    $ psql -d mydatabase
    

    You should probably be omitting that entirely and letting all the commands default to the user's name instead.

    $ sudo -u postgres createuser -s $USER
    $ createdb
    $ psql
    
  2. Using the SQL administration commands, and connecting with a password over TCP

    $ sudo -u postgres psql postgres
    

    And, then in the psql shell

    CREATE ROLE myuser LOGIN PASSWORD 'mypass';
    CREATE DATABASE mydatabase WITH OWNER = myuser;
    

    Then you can login,

    $ psql -h localhost -d mydatabase -U myuser -p <port>
    

    If you don't know the port, you can always get it by running the following, as the postgres user,

    SHOW port;
    

    Or,

    $ grep "port =" /etc/postgresql/*/main/postgresql.conf
    

Sidenote: the postgres user

I suggest not modifying the postgres user.

  1. It's normally locked from the OS. No one is supposed to "log in" to the operating system as postgres. You're supposed to have root to get to authenticate as postgres.
  2. It's normally not password protected and delegates to the host operating system. This is a good thing. This normally means in order to log in as postgres which is the PostgreSQL equivalent of SQL Server's SA, you have to have write-access to the underlying data files. And, that means that you could normally wreck havoc anyway.
  3. By keeping this disabled, you remove the risk of a brute-force attack through a named super-user. Concealing and obscuring the name of the superuser has advantages.
2 of 7
66

By default, you would need to use the postgres user:

sudo -u postgres psql postgres
🌐
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 - Learn how to connect to a PostgreSQL database from Linux and Windows using the psql command-line tool or the pgAdmin 4 GUI.
🌐
w3resource
w3resource.com › PostgreSQL › connect-to-postgresql-database.php
Connect to PostgreSQL Database on Linux, Windows | w3resource
April 1, 2026 - Typically initdb creates a table ... type the following command. Debian based systems like Ubuntu : Connect/login as root - user@user-pc:~$ sudo -i -u postgres postgres@user-pc:~$ psql psql (9.3.5, server 9.3.6) Type "help" ...
🌐
Stack Overflow
stackoverflow.com › questions › 73784377 › logging-into-postgresql-on-linux
ubuntu - Logging into PostgreSQL on Linux - Stack Overflow
Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... (base) irbis@irbis-System-Product-Name:~$ sudo su - postgres [sudo] password for irbis: postgres@irbis-System-Product-Name:~$ psql psql (13.8 (Ubuntu 13.8-1.pgdg20.04+1)) Type "help" for help.
🌐
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

🌐
DigitalOcean
digitalocean.com › community › tutorials › how-to-install-and-use-postgresql-on-ubuntu-18-04
How To Install and Use PostgreSQL on Ubuntu 18.04 | DigitalOcean
February 18, 2026 - To follow along with this tutorial, you will need one Ubuntu 18.04 server that has been configured by following our Initial Server Setup for Ubuntu 18.04 guide. After completing this prerequisite tutorial, your server should have a non-root user with sudo permissions and a basic firewall. Ubuntu’s default repositories contain Postgres packages, so you can install these using the apt packaging system.
Top answer
1 of 3
1

When trying to connect via pgadminIII i am prompted for a password when using the same user name.

pgAdminIII is probably connecting over TCP (as opposed to unix domain sockets) and so is subject to the rules of a 'host*' line from pg_hba.conf, rather than a 'local' line. If that line says a password is needed, then it will ask for a password--even if no password is actually assigned to the user. There should be a message in the server log describing the situation in more detail. The details are not sent to the (failed) client, to avoid leaking sensitive information.

Additionally, I would like to make my own user name an admin and use my linux password

It sounds like you might want PAM authentication (or maybe GSSAPI or LDAP). But to use those, you have to change your linux log-on system so that it also uses one of them, as I don't think any of them are configured to do that by default. You can also use peer authentication, which doesn't require your password explicitly, it just uses the fact that you are logged on as the local Linux user (which presumably did require the password) to authenticate to PostgreSQL. Or you could take the easy way out, and just set your PostgreSQL password to be the same thing as your linux password, and then manually keep them in sync.

However when atempting to connect form command line it acts like there should also be a database named the same as my user.

Yes, that is what happens if you don't specify a database name--it assumes you want to connect to a database with the same name as the user. If you want to connect to a different database, then just specify it.

2 of 3
1

Users, roles, and permissions can get kind of confusing in PostgreSQL. To control who can try to connect to the database, Postgres uses the pg_hba.conf file. In Ubuntu, it's buried in /etc/postgres/<#>/main.

From inside the data base there are roles which control permissions each user has inside the database.

An easy, but probably insecure way to connect is to set up trust from localhost connections in pg_hba.conf.

🌐
Ubuntu
help.ubuntu.com › community › PostgreSQL
PostgreSQL - Community Help Wiki
August 28, 2015 - sudo -u postgres createuser --superuser $USER sudo -u postgres psql ... Client programs, by default, connect to the local host using your Ubuntu login name and expect to find a database with that name too.
🌐
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 - Now attempt to connect to the PostgreSQL server again using the specified host, user, and database, it should allow the connection. $ psql -U ravi -h 192.168.0.124 -p 5432 -d ubuntumint
🌐
LogRocket
blog.logrocket.com › home › setting up a remote postgres database server on ubuntu 18.04
Setting up a remote Postgres database server on Ubuntu 18.04 - LogRocket Blog
June 4, 2024 - Note: server_user is your server user you would like to log in with server_ip is the IP address of your server · Then input your relevant user password or SSH key password if any. Next, update your server packages and dependencies by running: ... This will install Postgres along with its associated dependencies.
🌐
Boundlessgeo
docs.boundlessgeo.com › suite › 1.1.1 › dataadmin › pgGettingStarted › firstconnect.html
Connecting to PostgreSQL on Linux for the first time — Boundless Server 1.1.1 User Manual
To test your connection using pgAdmin, connect to the database at localhost:5432 using the user name postgres and the password supplied.
Top answer
1 of 13
369

The other answers were not completely satisfying to me. Here's what worked for postgresql-9.1 on Xubuntu 12.04.1 LTS.

  1. Connect to the default database with user postgres:

    sudo -u postgres psql template1

  2. Set the password for user postgres, then exit psql (Ctrl-D):

    ALTER USER postgres with encrypted password 'xxxxxxx';

  3. Edit the pg_hba.conf file:

    sudo vim /etc/postgresql/9.1/main/pg_hba.conf

    and change "peer" to "md5" on the line concerning postgres:

    local      all     postgres     peer md5

    To know what version of postgresql you are running, look for the version folder under /etc/postgresql. Also, you can use Nano or other editor instead of VIM.

  4. Restart the database :

    sudo /etc/init.d/postgresql restart

    (Here you can check if it worked with psql -U postgres).

  5. Create a user having the same name as you (to find it, you can type whoami):

    sudo createuser -U postgres -d -e -E -l -P -r -s <my_name>

    The options tell postgresql to create a user that can login, create databases, create new roles, is a superuser, and will have an encrypted password. The really important ones are -P -E, so that you're asked to type the password that will be encrypted, and -d so that you can do a createdb.

    Beware of passwords: it will first ask you twice the new password (for the new user), repeated, and then once the postgres password (the one specified on step 2).

  6. Again, edit the pg_hba.conf file (see step 3 above), and change "peer" to "md5" on the line concerning "all" other users:

    local      all     all     peer md5

  7. Restart (like in step 4), and check that you can login without -U postgres:

    psql template1

    Note that if you do a mere psql, it will fail since it will try to connect you to a default database having the same name as you (i.e. whoami). template1 is the admin database that is here from the start.

  8. Now createdb <dbname> should work.

2 of 13
66

Under Linux PostgresQL is usually configured to allow the root user to login as the postgres superuser postgres from the shell (console or ssh).

$ psql -U postgres

Then you would just create a new database as usual:

CREATE ROLE myuser LOGIN password 'secret';
CREATE DATABASE mydatabase ENCODING 'UTF8' OWNER myuser;

This should work without touching pg_hba.conf. If you want to be able to do this using some GUI tool over the network - then you would need to mess with pg_hba.conf.