It appears that your package manager failed to create the database named $user for you. The reason that

psql -d template1

works for you is that template1 is a database created by postgres itself, and is present on all installations. You are apparently able to log in to template1, so you must have some rights assigned to you by the database. Try this at a shell prompt:

createdb

and then see if you can log in again with

psql -h localhost

This will simply create a database for your login user, which I think is what you are looking for. If createdb fails, then you don't have enough rights to make your own database, and you will have to figure out how to fix the homebrew package.

Answer from Kirk Roybal on Stack Overflow
🌐
Medium
medium.com › @benpickles › database-postgres-does-not-exist-665a57d56514
Database “postgres” does not exist | by Ben Pickles | Medium
May 20, 2016 - Better to keep the existing data just in case this process needs to be rolled back: ... $ brew uninstall postgresql $ brew install postgresql $ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist ... If your luck is as good as mine then you should have a fully working PostgreSQL install with your previous database(s). I think this problem was caused by me not reading the instructions and blindly upgrading PostgreSQL with brew upgrade postgresql to go from 9.4 to 9.5.
Discussions

FATAL: database "postgres" does not exist
Yes - do NOT ever delete the postgres database. You can create new copy of it but you should be backing up all databases on the system. Step 1 - reinstall or fix PostgreSQL Step 2 - install & configure pgbackrest for full & differential backups (supports compression & S3) More on reddit.com
🌐 r/SQL
3
10
April 8, 2019
postgresql - psql: fatal database does not exist error - Database Administrators Stack Exchange
I have a linux user account, postgresql role, and associated database, all called foo. from the linux user foo, I ran the command dropdb foo Now when I try to run psql from the linux user foo, I... More on dba.stackexchange.com
🌐 dba.stackexchange.com
postgresql - ERROR: database "dbname" does not exist - Database Administrators Stack Exchange
The database name is Blog_development (and the one below it). I was playing with rails and trying to learn from the online documentation. I wanted to start over and delete everything. When trying to delete it however, it says it doesn't exist. I'm brand new to PostgreSQL so I'm a bit lost, nothing ... More on dba.stackexchange.com
🌐 dba.stackexchange.com
December 26, 2012
FATAL: database "dave" does not exist
using the "psql" menu item (OSX 10.8.4) I get Last login: Sat Oct 19 07:59:28 on ttys009 /Applications/Postgres.app/Contents/MacOS/bin/psql ; exit; Daves-MacBook-Pro:~ dave$ /Applications... More on github.com
🌐 github.com
15
October 19, 2013
🌐
GitHub
github.com › langgenius › dify › issues › 8147
database "postgres" does not exist · Issue #8147 · langgenius/dify
September 9, 2024 - docker-db-1 | 2024-09-09 08:39:54.594 UTC [1] LOG: starting PostgreSQL 15.8 on aarch64-unknown-linux-musl, compiled by gcc (Alpine 13.2.1_git20240309) 13.2.1 20240309, 64-bit docker-db-1 | 2024-09-09 08:39:54.595 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 docker-db-1 | 2024-09-09 08:39:54.595 UTC [1] LOG: listening on IPv6 address "::", port 5432 docker-db-1 | 2024-09-09 08:39:54.606 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" docker-db-1 | 2024-09-09 08:39:54.721 UTC [37] LOG: database system was shut down at 2024-09-09 08:29:08 UTC docker-db-1 | 2024-09-09 08:39:54.794 UTC [1] LOG: database system is ready to accept connections docker-web-1 | pm2 launched in no-daemon mode (you can add DEBUG="*" env variable to get more messages)
Author   langgenius
Top answer
1 of 2
1

There are two different ways of creating a database in PostgreSQL. One is from the command line. The other is from the PostgreSQL console.

Command Line

In order to be able to create a database from the command line, you must first change to a user with rights to create a database. As you have shown with the \du command above, the user with those rights is postgres.

root@eric-desktop:/home/eric# su postgres
postgres@eric-desktop:/home/eric$ createdb prismatest2
postgres@eric-desktop:/home/eric$

As you can see, the creatdb command does not give any feedback. You need to confirm the creation by listing the current databases:

psql -U postgres -l
                                                 List of databases
    Name     |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges   

-------------+----------+----------+-------------+-------------+------------+-----------------+-----------------------

 postgres    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 

 prismatest  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 

 prismatest2 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 

PostgreSQL Console

Another possibility is to use the PostgreSQL console. To enter the console:

root@eric-desktop:/home/eric# sudo -u postgres psql psql (15.7 (Debian 15.7-0+deb12u1)) Type "help" for help.

postgres=# 
postgres=# CREATE DATABASE "prismatest3";
CREATE DATABASE
postgres=# 

To confirm the creation of the database, list the databases:

postgres=# \l

                                                 List of databases
    Name     |  Owner   | Encoding |   Collate   |    Ctype    | ICU Locale | Locale Provider |   Access privileges   

-------------+----------+----------+-------------+-------------+------------+-----------------+-----------------------

 postgres    | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 

 prismatest  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 

 prismatest2 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 

 prismatest3 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 |            | libc            | 
2 of 2
3

Notice how the prompt has changed from the first line to the second:

        v
postgres=# createdb prismatest
postgres-# \l
        ^

psql is hinting that you are entering more lines of the same command.

Enter the semicolon (";") that terminates the create database statement.

postgres=# createdb prismatest ; 
🌐
Oracle DBA Training
learnomate.org › home › postgresql › fatal: database “dbname” does not exist
5 Simple Fixes for PostgreSQL Database Does Not Exist Error
September 19, 2025 - Getting the error “FATAL: PostgreSQL database does not exist” in PostgreSQL? Learn 5 simple fixes to create missing databases, assign owners, and resolve connection issues quickly.
🌐
DigitalOcean
docs.digitalocean.com › how do i fix the "database does not exist" error when connecting to postgresql?
How do I fix the "Database Does Not Exist" error when connecting to PostgreSQL? | DigitalOcean Documentation
December 20, 2021 - A Database Does Not Exist error ... to the wrong cluster: ... If you are getting this error, first verify that you are using the correct hostname to connect by following How to Connect to PostgreSQL Database Cluste...
🌐
Reddit
reddit.com › r/sql › fatal: database "postgres" does not exist
r/SQL on Reddit: FATAL: database "postgres" does not exist
April 8, 2019 -

Hi guys, I've been googling around on how to solve the problem with resolving my issue into getting back into postgresql 11...

I dropped the postgres database in my software then it led to me not able to reconnect to the server.

I've tried doing some code on the command line with adding a database and entering password but not working or recovering the access with psql -u postgres.

None haven't worked. IDK where to go because I'ved seen like several sources and no luck. I wanna resume coding on postgresql.

Thanks...

Find elsewhere
🌐
GitHub
github.com › PostgresApp › PostgresApp › issues › 139
FATAL: database "dave" does not exist · Issue #139 · PostgresApp/PostgresApp
October 19, 2013 - Last login: Sat Oct 19 07:59:28 on ttys009 /Applications/Postgres.app/Contents/MacOS/bin/psql ; exit; Daves-MacBook-Pro:~ dave$ /Applications/Postgres.app/Contents/MacOS/bin/psql ; exit; psql: FATAL: database "dave" does not exist logout
Author   PostgresApp
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 1530716 › unable-to-access-postgresql-database-after-restore
Unable to access PostgreSQL database after restore - 'database does not exist' error - Microsoft Q&A
February 12, 2024 - Seems like Your Database was created under temporary tablespace, this temporary tablespace is created by default in Azure database for PostgreSQL Flexible Server to store the temporary files. As this tablespace is only meant to store temporary files, we do not back it up during maintenance activities or server restarts.
🌐
Reddit
reddit.com › r/postgresql › database does not exist
r/PostgreSQL on Reddit: Database does not exist
August 9, 2022 -

Hello everyone, I had a python Django project I worked on a while back, I am trying to revisit this project but I am running into issues when I try to run the server locally. Not sure what is causing this issue.
in the second screenshot It shows that the database cannot be found! However the deployed version of the website seems to still work, I just added the third entry in the screenshot.
Note: I was using PostgreSQL13 with this project, How can I verify if the database is still there or was erased( deployed version is working!) ?

🌐
CommandPrompt Inc.
commandprompt.com › education › postgresql-create-database-if-not-exists
PostgreSQL CREATE DATABASE IF NOT EXISTS — CommandPrompt Inc.
May 5, 2024 - To achieve the functionality of the “IF NOT EXISTS” option, a subquery can be used in Postgres. For this purpose, you can specify the NOT EXIST operator in the WHERE clause to check if the desired database already exists.
Address   2950 Newmarket ST STE 101 - 231, 98226, Bellingham
🌐
IONOS
ionos.com › digital guide › server › configuration › solve the postgresql error "psql: fatal: database "root" does not exist"
Solve the PostgreSQL Error "psql: FATAL: database "root" does not exist" - IONOS
December 11, 2020 - When you log into Post­greSQL as any user other than the postgres user, it will attempt to log you into a database of the same name as your user account. This means that if you try to use the psql command as root, it will try to log you into the database root. If you try to log in while signed on as jdoe it will look for the database jdoe, and so forth. Unable to find this database, Post­greSQL gives the error message that "database [your username] does not exist."
🌐
AltCadeMy
altcademy.com › questions › fatal-database-postgres-does-not-exist
FATAL: database "postgres" does not exist | Altcademy™
When running rails server, I get the error FATAL: database "postgres" does not exist · [SOLUTION] Make sure you don't delete the default database postgres. Check your databases in psql: $ psql$ \l · To recreate database postgres in psql: $ ...
🌐
Reddit
reddit.com › r/postgresql › database current username does not exist issue
r/PostgreSQL on Reddit: Database current username does not exist issue
January 31, 2021 -

I have existing multiple *_development and *_test databases on postgres 12. Postgres by default creates a postgres user upon installation. I am always logged in as the current linux user on my system.

Why doesnt psql let me login into psql without specifying a specific database by throwing the error message psql: error: could not connect to server: FATAL: database "<current username>" does not exist?

Instead, the commands psql -d my_application_development and sudo -u postgres psql work even though the latter does not say which database

PS what is template0 and template1? was it a mistake in name

Top answer
1 of 2
2
When logging into a Postgres server with the psql client if you do not specify a database user with the -U option then it will default to using the current OS username for the database username it attempts to log\ in with. If there is not a database user with that name then it will fail. If you do not specify a database name to log into then it will default to looking for a database with the same name as the database user it is attempting to log in with and if there is not a database with that name then the login will fail. When a new cluster is created with initdb it always starts with three databases: postgres, template1, and template0. Here's info on the template databases. You can just ignore them. If you do not specify a superuser with the -U option when running initdb then it will use the current OS user name. By convention, most package managers and other install scripts will create an OS user named postgres and specify that the superuser database user account also be named postgres when initializing a new cluster with initdb. So: Why doesnt psql let me login into psql without specifying a specific database by throwing the error message psql: error: could not connect to server: FATAL: database "" does not exist? Instead, the commands psql -d my_application_development and sudo -u postgres psql work even though the latter does not say which database Because in the former case since you are not specifying the database name it is looking for a database with the same name as the database username it is attempting to log in with and the latter case works because there is a database named postgres to match the postgres user your are logging in with.
2 of 2
1
psql always tries to connect with the current logged in user's name. If you use sudo -u postgres, then it will connect with the postgres user. By default it tries to connect to the database with the same name as the user. The relevant page from the documentation about template0 and template1, https://www.postgresql.org/docs/current/manage-ag-templatedbs.html In short, template1 is used as a template when creating new databases, you can specify your own template db.
🌐
GitHub
github.com › PostgresApp › PostgresApp › issues › 511
Fatal database "xxx" does not exist? · Issue #511 · PostgresApp/PostgresApp
June 25, 2019 - org.postgresql.util.PSQLException: FATAL: database "config_ka" does not exist
Author   PostgresApp