Createuser: could not connect to database postgres: FATAL: role "tom" does not exist

psql: fatal: database does not exist
postgres create user
pg::connectionbad: fatal: role postgres'' does not exist
.psqlexception fatal role postgres does not exist
create role postgres mac
psql: fatal: role pi does not exist
pg_dumpall: could not connect to database template1'': fatal: role does not exist
postgres create user with superuser privileges

I'm trying to set up Postgres for the first time, and I need to create a user with permissions to read and create databases. However, when I use "createuser username" in my terminal I get the following message:

createuser: could not connect to database postgres: FATAL: role "tom" does not exist

Tom is my Ubuntu user account that I'm logged into right now. I'm trying to create a username of "postgres" then do a "psql -U psql template1" so I can create a database and assign an owner to it for my Rails app.

Any help?

You mentioned Ubuntu so I'm going to guess you installed the PostgreSQL packages from Ubuntu through apt.

If so, the postgres PostgreSQL user account already exists and is configured to be accessible via peer authentication for unix sockets in pg_hba.conf. You get to it by running commands as the postgres unix user, eg:

sudo -u postgres createuser owning_user
sudo -u postgres createdb -O owning_user dbname

This is all in the Ubuntu PostgreSQL documentation that's the first Google hit for "Ubuntu PostgreSQL" and is covered in numerous Stack Overflow questions.

(You've made this question a lot harder to answer by omitting details like the OS and version you're on, how you installed PostgreSQL, etc.)

Createuser could not connect to database postgres [Fix], This tutorial explains how to fix the error - Createuser could not connect to database postgres or createdb FATAL: role "root" does not exist. Then you have to connect using psql --username=OLDUSERNAME, or with psql --username=postgres to connect as the postgres user. psql also uses your environment variables for configuration. So for troubleshooting, type export in Terminal to make sure there are no settings for PGUSER etc. that override the defaults.

See git gist with instructions here

Run this:

 sudo -u postgres psql

OR

psql -U postgres

in your terminal to get into postgres

postgres=#

Run

CREATE USER new_username;

Note: Replace new_username with the user you want to create, in your case that will be tom.

postgres=# CREATE USER new_username;
CREATE ROLE

Since you want that user to be able to create a DB, you need to alter the role to superuser

postgres=# ALTER USER new_username SUPERUSER CREATEDB;
ALTER ROLE

To confirm, everything was successful,

postgres=# \du
                         List of roles
 Role name |                   Attributes                   | Member of 
-----------+------------------------------------------------+-----------
new_username     | Superuser, Create DB                           | {}
postgres         | Superuser, Create role, Create DB, Replication | {}
root             | Superuser, Create role, Create DB              | {}

postgres=# 

Update/Modification (For Mac):

I recently encountered a similar error on my Mac:

psql: FATAL: role "postgres" does not exist

This was because my installation was setup with a database superuser whose role name is the same as your login (short) name.

But some linux scripts assume the superuser has the traditional role name of postgres

How did I resolve this?

If you installed with homebrew run:

/usr/local/Cellar/postgresql/10.5/bin/createuser -s postgres

OR:

/usr/local/Cellar/postgresql/10.5/bin/createuser -s new_username

If you installed with postgres.app for Mac run:

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

P.S: replace 10.5 with your PostgreSQL version

Documentation: 8.3: Creating a Database, Another response could be this: createdb: could not connect to database postgres: FATAL: role "joe" does not exist. where your own login name is mentioned. createdb: could not connect to database template1: FATAL: role "Omistaja" does not exist. I started the postgreSQL service before executing the command, I did it from windows start menu and clicking the play sign with "Start postgresql" Am I doing something wrong?

sudo -u postgres createuser -s tom 

this should help you as this will happen if the administrator has not created a PostgreSQL user account for you. It could also be that you were assigned a PostgreSQL user name that is different from your operating system user name, in that case you need to use the -U switch.

After install I get psql: FATAL: role "me" does not exist · Issue #313 , After install I get psql: FATAL: role "me" does not exist #313 createuser -U postgres -s YOURUSERNAME createdb YOURUSERNAME enabled LOG: database system is ready to accept connections LOG: autovacuum  sudo -u postgres createuser -s tom this should help you as this will happen if the administrator has not created a PostgreSQL user account for you. It could also be that you were assigned a PostgreSQL user name that is different from your operating system user name, in that case you need to use the -U switch.

Your error is posted in the official documentation. You can read this article.

I have copied the reason for you (and hyperlinked the URLs) from that article:

This will happen if the administrator has not created a PostgreSQL user account for you. (PostgreSQL user accounts are distinct from operating system user accounts.) If you are the administrator, see Chapter 20 for help creating accounts. You will need to become the operating system user under which PostgreSQL was installed (usually postgres) to create the first user account. It could also be that you were assigned a PostgreSQL user name that is different from your operating system user name; in that case you need to use the -U switch or set the PGUSER environment variable to specify your PostgreSQL user name

For your purposes, you can do:

1) Create a PostgreSQL user account:

sudo -u postgres createuser tom -d -P

(the -P option to set a password; the -d option for allowing the creation of database for your username 'tom'. Note that 'tom' is your operating system username. That way, you can execute PostgreSQL commands without sudoing.)

2) Now you should be able to execute createdb and other PostgreSQL commands.

psql: FATAL: role "postgres" does not exist, Not a full answer, but consider the manual about initdb here: -U username --​username= username. Selects the user name of the database  If you wish to create a new superuser, you must connect as a superuser, not merely with CREATEROLE privilege. Being a superuser implies the ability to bypass all access permission checks within the database, so superuserdom should not be granted lightly. createuser is a wrapper around the SQL command CREATE ROLE. There is no effective difference between creating users via this utility and via other methods for accessing the server.

1- Login as default PostgreSQL user (postgres)

sudo -u postgres -i

2- As postgres user. Add a new database user using the createuser command

[postgres]$ createuser --interactive

3-exit

[postgres]$ exit

[PostgreSQL] Can't create a database with createdb, createuser: could not connect to database postgres: FATAL: role "username" does not exist. According to createuser man page: createuser creates a new  createuser: could not connect to database postgres: FATAL: password authentication failed for user "postgres" when i posted the question on dspace mailing list..one of the guy replied me with this solution.. createuser -h localhost -U postgres -d -A -P dspace but he said that to execute the above command i must know the password for the user postgres..

PostgreSQL:createdb: could not connect to database postgres , response could be like this: ? 1. createdb: could not connect to database postgres: FATAL: role  `repmgr` is a suite of open-source tools to manage replication and failover within a cluster of PostgreSQL servers. It enhances PostgreSQL's built-in replication capabilities with utilities to set up standby servers, monitor replication, and perform administrative tasks such as failover or switchover operations.

Getting Started With PostgreSQL, PG actually owns that command name) but I still get createuser: could not connect to database postgres: FATAL: role "root" does not exist  Once you’re done assigning roles, you can go on with your day-to-day database activity. I use Postico for managing my Postgres databases — I personally love the simple and intuitive layout.

PostgreSQL Error, Database Research & Development: Shared truth about the PostgreSQL Error - FATAL: database role "root" does not exist You should use default Postgres user for your first database connection. A PostgreSQL Fatal error like role or username does not exist. sudo -u postgres createuser dbrnd -d -P 

Comments
  • Are you really trying to create a 'postgres' user? Or just a user for your Rails app? Normally the 'postgres' user already exists and the problem is just connecting to it in order to do what you need.
  • well after creating owning_user, How do yo connect with this user? I mean to connect default user i use , sudo -u postgres psql postgres . same way how to connect owning_user?
  • Three options. (a) Set a password for that user and edit pg_hba.conf to use md5 auth for that user (see the manual); (b) Create an OS user by the same name and keep on using peer auth; or (c) more advanced, create a pg_ident.conf mapping to allow your OS user to log in as the new user.
  • This worked to get into postgres: psql -U postgres
  • @AleksandrH Glad it worked for you
  • This is completely incorrect. If the user hadn't run initdb they wouldn't have a database server running and accepting connections in order to produce the error message reported. Please remove this misleading answer. (BTW, in future, please link to /docs/current/static/ to avoid stale links accumulating)