751

I'm a postgres novice.

I installed the postgres.app for mac. I was playing around with the psql commands and I accidentally dropped the postgres database. I don't know what was in it.

I'm currently working on a tutorial: http://www.rosslaird.com/blog/building-a-project-with-mezzanine/

And I'm stuck at sudo -u postgres psql postgres

ERROR MESSAGE: psql: FATAL: role "postgres" does not exist

$ which psql

/Applications/Postgres.app/Contents/MacOS/bin/psql

This is what prints out of psql -l

                                List of databases
    Name    |   Owner    | Encoding | Collate | Ctype |     Access privileges     
------------+------------+----------+---------+-------+---------------------------
 user       | user       | UTF8     | en_US   | en_US | 
 template0  | user       | UTF8     | en_US   | en_US | =c/user                  +
            |            |          |         |       | user      =CTc/user      
 template1  | user       | UTF8     | en_US   | en_US | =c/user                  +
            |            |          |         |       | user      =CTc/user      
(3 rows)

So what are the steps I should take? Delete an everything related to psql and reinstall everything?

Thanks for the help guys!

3
  • 3
    Restarting Computer. Ensures Launchd from brew services start postresql is executed. Commented Jun 26, 2017 at 12:35
  • 7
    Postgresapp use your computer's login username instead of postgres. You can find out what is your username by typing whoami in the terminal. Use this username to login instead.
    – duykhoa
    Commented Jul 21, 2022 at 19:16
  • while installing through brew it also use your computer's login username instead of postgres. so, just login by using your username at terminal or while making connection through pgAdmin4 Thanks @duykhoa Commented Apr 16, 2023 at 13:32

36 Answers 36

674

NOTE: If you installed postgres using homebrew, see the comments from @user3402754 and @originalhat below.

Note that the error message does NOT talk about a missing database, it talks about a missing role. Later in the login process it might also stumble over the missing database.

But the first step is to check the missing role: What is the output within psql of the command \du ? On my Ubuntu system the relevant line looks like this:

                              List of roles
 Role name |            Attributes             | Member of 
-----------+-----------------------------------+-----------
 postgres  | Superuser, Create role, Create DB | {}        

If there is not at least one role with superuser, then you have a problem :-)

If there is one, you can use that to login. And looking at the output of your \l command: The permissions for user on the template0 and template1 databases are the same as on my Ubuntu system for the superuser postgres. So I think your setup simple uses user as the superuser. So you could try this command to login:

sudo -u user psql user

If user is really the DB superuser you can create another DB superuser and a private, empty database for him:

CREATE USER postgres SUPERUSER;
CREATE DATABASE postgres WITH OWNER postgres;

But since your postgres.app setup does not seem to do this, you also should not. Simple adapt the tutorial.

10
  • 14
    For me, the user was not being created - this worked instead CREATE USER postgres WITH SUPERUSER PASSWORD 'password'; am using postgres v10.
    – arcseldon
    Commented Nov 4, 2017 at 1:10
  • 592
    If you installed Postgres from homebrew then you need to run /usr/local/opt/postgres/bin/createuser -s postgres in your terminal Commented Sep 5, 2018 at 18:36
  • 27
    I had to run /usr/local/opt/postgresql/bin/createuser -s postgres Commented Aug 15, 2019 at 2:46
  • 28
    If you are using a specific version of postgres, then you need to include the version in the path -- /usr/local/opt/postgresql@11/bin/createuser -s postgres
    – Digitrance
    Commented Apr 21, 2020 at 4:38
  • 140
    For M1 Macs: /opt/homebrew/bin/createuser -s postgres Commented Jun 6, 2021 at 16:01
342

For MAC:

  1. Install Homebrew
  2. brew install postgres
  3. initdb /usr/local/var/postgres
  4. /usr/local/Cellar/postgresql/<version>/bin/createuser -s postgres or /usr/local/opt/postgres/bin/createuser -s postgres which will just use the latest version.
  5. start postgres server manually: pg_ctl -D /usr/local/var/postgres start

To start server at startup

  • mkdir -p ~/Library/LaunchAgents
  • ln -sfv /usr/local/opt/postgresql/*.plist ~/Library/LaunchAgents
  • launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

Now, it is set up, login using psql -U postgres -h localhost or use PgAdmin for GUI.

By default user postgres will not have any login password.

Check this site for more articles like this: https://medium.com/@Nithanaroy/installing-postgres-on-mac-18f017c5d3f7

3
  • 8
    With the newest brew install you can do brew services start postgresql to start postgres
    – Automatico
    Commented Jan 20, 2017 at 12:40
  • 1
    Step 4 could be /usr/local/opt/postgresql@<version>/bin/createuser -s postgres
    – varontron
    Commented Mar 21, 2021 at 2:07
  • Step 3 gives Permission denied
    – Stewart
    Commented Dec 15, 2022 at 11:07
295

The key is "I installed the postgres.app for mac." This application sets up the local PostgreSQL installation with a database superuser whose role name is the same as your login (short) name.

When Postgres.app first starts up, it creates the $USER database, which is the default database for psql when none is specified. The default user is $USER, with no password.

Some scripts (e.g., a database backup created with pgdump on a Linux systsem) and tutorials will assume the superuser has the traditional role name of postgres.

You can make your local install look a bit more traditional and avoid these problems by doing a one time:

/Applications/Postgres.app/Contents/Versions/9.*/bin/createuser -s postgres

which will make those FATAL: role "postgres" does not exist go away.

12
  • 141
    If you're using postgres from Homebrew, then you want /usr/local/Cellar/postgresql/9.2.4/bin/createuser -s postgres (for version 9.2.4, obviously). Commented Mar 13, 2014 at 14:21
  • 1
    For postgres.app 9.3, they seem to have re-arranged the directories. I tried: /Applications/Postgres.app/Contents/Versions/9.3/bin/createuser -s postgres, but this generated the same eror message: "createuser: could not connect to database postgres: FATAL: role "postgres" does not exist" Commented Sep 25, 2014 at 23:22
  • 8
    /Applications/Postgres.app/Contents/Versions/9.3/bin/createuser -s postgres ran correctly for me.
    – cjspurgeon
    Commented Nov 13, 2014 at 22:31
  • 16
    @RogerLipscombe if you run brew link postgresql after the installation, there's no need to append the whole path to createuser, a simple createuser -s postgres will work great
    – AlessioX
    Commented Sep 7, 2016 at 14:39
  • 7
    createuser -s postgres worked for me. I installed postgres with Homebrew
    – biniam
    Commented Jan 8, 2019 at 11:47
188

And if you are here in 2024 :) and wondering what works with the latest Postgres on the latest macOS (macOS Monterey )

follow this:

brew install postgresql
createuser -s postgres
brew services restart postgresql

Note: In case you are using a dockerized instance make sure to first set the variables so you can connect to that instance: export PGUSER="youruser" export PGHOST="127.0.0.1" export PGPASSWORD="yourpassword.

Thanks, @NicolasZ for the comment regarding dockerized instance.

8
  • 6
    that worked nicely running on Monterey. Commented Feb 14, 2022 at 22:26
  • 2
    Thanks!, i uninstall postgres and then i did what you say, it worked! Commented Apr 21, 2022 at 11:11
  • 1
    thank you very much! I just upgraded to mac OS v13 Ventura today and my postgres stopped working. I had to uninstall and reinstall it via brew (somehow upgrading it wasn't enough) and follow your suggestions. the /usr/local/opt/postgres/bin/createuser -s postgres command mentioned in other answers also didn't work.
    – charint
    Commented Oct 26, 2022 at 1:57
  • 2
    Life saver ! it works for me (Mac M2) Commented May 14 at 2:10
  • 2
    Thank you so much for this!!! This should be the top answer!
    – solz
    Commented May 21 at 16:28
96
createuser postgres --interactive

or make a superuser postgresl just with

createuser postgres -s
3
  • Cool stackoverflow.com/questions/17633422/…
    – unom
    Commented Oct 26, 2017 at 10:02
  • I found this answer useful when my pgadmin kept on asking for postgres user and I dint have postgres role to begin with, this command created it, I added the autogenerated pw into my pgadmin and problem solved!
    – theusual
    Commented Apr 3, 2019 at 21:43
  • This works for me.. but this way: "create user postgres" Commented Jun 14, 2019 at 1:47
64

This happens when you run initdb with a user whose ID is not postgres, without specifying the postgres username with --username=postgres or -U postgres.

The database cluster is then created with the system's user account that you used to run initdb, and it is given superuser permissions.

To fix it, simply create a new user named postgres with the option --superuser using the createuser utility that comes with Postgres. The utility can be found in the Postgres' bin directory. e.g.

createuser --superuser postgres

If you have a custom hostname or port then be sure to set the appropriate options.

Don't forget to delete the other user account that was created for you by initdb.

1
  • 2
    This is the exact explanation I was looking for! Thanks.
    – Nitin Nain
    Commented Aug 5, 2023 at 8:18
48

If you installed postgres from brew, run this in your terminal :

/usr/local/opt/postgres/bin/createuser -s postgres
3
  • 1
    Exactly what I was looking for.
    – Tvaroh
    Commented Apr 1, 2021 at 9:11
  • Thank you for this. This worked for me while setting up a server in pgAdmin 4.
    – iarobinson
    Commented Dec 7, 2021 at 17:19
  • For me the path was different /opt/homebrew/opt/postgresql@14/bin/createuser -s postgres
    – sh6210
    Commented Mar 28, 2023 at 18:17
24

First you need create a user:

sudo -u postgres createuser --superuser $USER

After you create a database:

sudo -u postgres createdb $USER

Change $USER to your system username.

You can see the the complete solution here.

1
  • I was receiving this error: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL: role "jose.mateus" does not exist None of the above worked for me, in case you are getting the same error as me, this option here helps Commented Jul 28, 2022 at 17:19
23

I needed to unset $PGUSER:

$ unset PGUSER
$ createuser -s postgres
0
18

If you installed postgres from Brew and are using an Apple Silicon (M1) mac, run this in your terminal:

/opt/homebrew/opt/postgresql/bin/createuser -s postgres

If you're using an Intel (x86) mac, run this in your terminal:

/usr/local/opt/postgres/bin/createuser -s postgres
1
  • /usr/local/opt/postgresql@14/bin/createuser -s postgres for postgres 14 Commented Oct 13, 2022 at 3:40
17

For me, this code worked:

/Applications/Postgres.app/Contents/Versions/9.4/bin/createuser -s postgres

it came from here: http://talk.growstuff.org/t/fatal-role-postgres-does-not-exist/216/4

1
  • Does it mean this new posgres user won't have a password? Commented Nov 30, 2019 at 16:23
10

This article helped me to solve same issue psql: FATAL: role “postgres” does not exist.

I am using mac, so I entered this command in terminal:

createuser -s postgres

And it worked for me.

9

Running this on the command line should fix it

/Applications/Postgres.app/Contents/Versions/9.4/bin/createdb <Mac OSX Username Here>

1
  • 1
    This works for me in MacOS 11.1. Thank you!
    – A77ak
    Commented Apr 26, 2021 at 12:39
9

If you're using docker, make sure you're NOT using POSTGRES_USER=something_else, as this variable is used by the standard image to know the name of the PostgreSQL admin user (default as postgres).

In my case, I was using this variable with the intent to set another user to my specific database, but it ended up of course changing the main PostgreSQL user.

9

We have a db named postgres after brew install postgresql and brew services start postgresql. So we can open psql like this by default.

psql postgres

And then we can add users with any name like this in that psql console.

CREATE USER postgres

And if we want a super user, then we can add SUPERUSER at the end.

1
  • 1
    Good instruction. 'psql postgres' is connecting the postgres database(default) within the current user
    – Keating
    Commented Nov 12, 2021 at 5:21
9

This worked for me

createuser -s postgres

note: I'm using mac catalina

6

For m1 chips, if you have not installed postgresql package by homebrew, install it in terminal with:

brew install postgre

then create a username manually by:

/opt/homebrew/bin/createuser -s <username> 

your error is probably fixed; but if you occur the error

FATAL: database "databasename" does not exist

then you have to create your database manually by:

/opt/homebrew/bin/createdb -U <username>  <databasename>    
5

My System

  • Machine: M2 Macbook Pro
  • macOS: Ventura 13.4.1
  • Postgres Version: 15.3 (postgres --version)
  • Installation Method: Homebrew

My Solution

I was able to resolve the issue by creating a postgres user via the following command. Please note your postgres location and createuser utility may vary based on your system and/or installation method:

/opt/homebrew/opt/postgresql@15/bin/createuser -s postgres
4

Dropping the postgres database doesn't really matter. This database is initially empty and its purpose is simply for the postgres user to have a kind of "home" to connect to, should it need one.

Still you may recreate it with the SQL command CREATE DATABASE postgres;

Note that the tutorial mentioned in the question is not written with postgres.app in mind. Contrary to PostgreSQL for Unix in general, postgres.app tries to look like a normal application as opposed to a service that would be run by a dedicated postgres user having different privileges than your normal user. postgres.app is run and managed by your own account.

So instead of this command: sudo -u postgres psql -U postgres, it would be more in the spirit of postgres.app to just issue: psql, which automatically connects to a database matching your users's name, and with a db account of the same name that happens to be superuser, so it can do anything permissions-wise.

4
  • Okay. But why do I get an error ERROR: cannot drop the currently open database when I try to delete the user db? So is it not a good practice to use this postgres.app?
    – user805981
    Commented Mar 8, 2013 at 21:10
  • You cannot drop a db while connected to it. why would you want to delete it, anyway? It's here for your convenience. Commented Mar 8, 2013 at 21:15
  • I just wanted to see if I could. So it's like a default database correct when I boot up my psql and postgres.app? Now what about the access privileges? I am not able to set them anymore? I see that template0 and template1 has access privileges
    – user805981
    Commented Mar 8, 2013 at 21:29
  • @user805981 Postgres.app is for testing and development purposes; it's a PostgreSQL package from Heroku that's designed to make it easier for Mac users developing with Ruby on Rails to test with PostgreSQL instead of the default SQLite. Rails uses tended to test with SQLite and deploy to PostgreSQL which caused all sorts of problems, so Heroku tried to make it easier to test on PostgreSQL too. Postgres.app is fine for testing, but I wouldn't consider using it for production or real data, that's just not what it's for. Commented Mar 9, 2013 at 0:32
4

This is the only one that fixed it for me :

createuser -s -U $USER
1
  • 3
    This makes the current user a system superuser rather than just a postgres superuser. I would say this would be generally a bad idea as it circumvents the usual security policies. You should user sudo to gain temporary superuser privileges. Commented Mar 29, 2016 at 21:54
4

For what it is worth, i have ubuntu and many packages installed and it went in conflict with it.

For me the right answer was:

sudo -i -u postgres-xc
psql
4

I've faced similar problem today, actually i was not sure what was the username. Here is the 2 thing, if you are under enterprise and don't have system admin access the postgres will create your enterprise username as the postgres admin username. If you install through Homebrew it will definitely happening. In that case simply run your psql service with brew and do an echo of the username

brew services start postgresql

then

echo $USER

You will see your username of the postgres user.

2
  • damn. this actually worked. Thanks Commented Jan 27, 2022 at 17:20
  • This also worked for me on M1 Macbook... the postgres user was the username of my logged in account
    – Joshua W
    Commented Sep 8, 2022 at 18:23
4

On Mac, executing

createuser -s postgres

in a terminal worked.

3

With a new mac (M1) and latest postgres (14.0) installed via homebrew, nothing helped me from this topic, but i just reinstalled postgres and it helped:

brew services stop postgresql
rm -rf /opt/homebrew/var/postgres/*
brew reinstall postgresql
initdb --locale=C -E UTF-8 /opt/homebrew/var/postgres
brew services restart postgresql

So, it's a miracle or something like that...

Then just:

psql -d postgres
2
  • Btw, i installed postgres earlier, but not used about 2 weeks, may be it was a bug or something else and it was fixed, i have no idea, but it works Commented Nov 5, 2021 at 8:39
  • You should not use that as you will install a random version of postgresql (most likely latest available on brew) Commented Jan 19, 2023 at 17:37
2

If you are experiencing this problem right after running a docker container try destroying the container and recreating it. That solved it for me:

docker-compose down
docker-compose up --force-recreate

This should recreate the db with postgresuser as default user

2

If you are a MAC (M1) user and installed the Postgres using HomeBrew then follow these steps:

  1. Check your Postgres location using which psql
  2. then run the command /opt/homebrew/bin/createuser -s postgres if the output for the first command is /opt/homebrew/bin/psql

The idea is to create a user named 'postgres' using the Postgres installation location. So you may need to change the command based on the location of your Postgres.

2

Context

I am adding an answer for a case I have not seen here, which is an edge case if you have multiple users on the same machine and the user who is trying to use postgres services is not the user who installed postgres on the machine.

What I have tried

Among other similar commands, for me all these commands failed:

createuser -s [your username]
# createuser: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  role "[your username]" does not exist
createuser -s postgres
# createuser: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  role "[your username]" does not exist
sudo -u postgres createuser --superuser [your username]
# sudo: unknown user: postgres
# sudo: error initializing audit plugin sudoers_audit
psql -U postgres
# psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: FATAL:  role "postgres" does not exist

Reason

The reason is because neither postgres role nor [your username] (aka whoami on your command line) are in postgres.

Solution

In such edge case I had to first login with the user who installed postgres:

sudo su - [username that installed postgres]

And then create a role for my new user:

createuser -s [your username]
1
  • it is amazing how my Stackoverflow answer comes in handy to myself exactly one year later and boom!
    – Fed
    Commented Jan 11 at 14:30
2

Go to your terminal. Make sure that you're out of the postgres shell and your postgresql is running in the terminal. Now run the following command in your terminal :

createuser -s postgres

2

In my case I only needed to create that user in the database I was restoring:

CREATE ROLE postgres;
GRANT ALL PRIVILEGES ON DATABASE database_name TO postgres;
1

On Ubuntu system, I purged the PostgreSQL and re-installed it. All the databases are restored. This solved the problem for me.

Advice - Take the backup of the databases to be on the safer side.

Not the answer you're looking for? Browse other questions tagged or ask your own question.