Postgresql: FATAL: role does not exist [duplicate] - postgresql

This question already has answers here:
psql: FATAL: role "postgres" does not exist
(32 answers)
Closed 20 days ago.
I'm setting up Postgresql for use with a Rails app, but I do not seem to be able to connect to, or properly configure, the database (the errors I get after starting the Rails server are: ActiveRecord::NoDatabaseError and could not translate host name "MyProfile" to address: nodename nor servname provided, or not known).
I gather from the dozens of other questions on this topic that I need to switch to, or create the "MyComputer" role, however I've noticed that they all require using the psql command. When I run even just psql I, again, get the error FATAL: role "MyProfile" does not exist.
So far I've been following Heroku's instal process and am stuck here (or more accurately here, after the installation, where Heroku says that running psql -h localhost should work). Am I missing an obvious step here/doing something wrong?
I've also tried:
sudo su - MyProfile
variations of sudo -u postgres createuser owning_user
and a couple other commands in an effort to create this roll/user, but I can't seem to get done what I need to to resolve the issue.
EDIT
I've also run ps aux | grep postgres and it looks like all the PID's that are associated with anything postgres are running under "MyProfile" (assuming I'm reading it right). But the psql command still returns that the role does not exist. #sadface
EDIT 2
I just opened the Postgres App and clicked the "Open psql" button. It opened the Terminal, ran a command ('/Applications/Postgres.app/Contents/Versions/9.5/bin'/psql -p5432) and then gave me the same error (psql: FATAL: role "MyProfile" does not exist). Which perhaps suggests to me that it's an system issue, and not a Rails issue at all?
Edit 3
This is most certainly a pg issue, not a rails issue. I just uninstalled the app, reinstalled it using SQLite (yucks), ran the local server and got the test landing page to show up. So the problem appears to be with my local machine but not the app itself. Removed RoR tag, and still looking for answers from Postgres gurus on why the role issue persists :)

I ran into similar issues when setting a new Rails application with Postgresql. I got the following error messages below
FATAL: role "promisepreston" does not exist
Couldn't create 'MyBlog_development' database. Please check your configuration.
rails aborted!
ActiveRecord::NoDatabaseError: FATAL: role
Caused by:
PG::ConnectionBad: FATAL: role "promisepreston" does not exist
To solve this simply follow the solution below
First, we need to login to the postgres user account via the command line interface;
sudo su - postgres
Next, connect to the database server using the psql client, as the postgres role:
psql -U postgres
Welcome to psql 10.6, the PostgreSQL interactive terminal.
postgres#Preston-PC:~$ psql -U postgres
psql (10.6 (Ubuntu 10.6-0ubuntu0.18.04.1))
Type "help" for help
postgres=#
Next, connected with the psql client, we’ll create a role with our desired rolename that has the LOGIN attribute and our desired password, and that can create databases and manage roles (N/B: Please do not type this postgres=#, since it's a placeholder):
postgres=# create role rolename with createdb login password 'password1';
Note the required trailing semicolon ( ; ) at the end of the SQL statement. The single-quotes ( ‘ ‘ ) are not part of the password, but must enclose it.
Did it work? You can check using \du command (N/B: Please do not type this postgres=#, since it's a placeholder):
postgres=# \du
You can now run the command to create the database for your Rails application;
rails db:create
And then also run the command to migrate the database for your Rails application;
rails db:migrate
That's all.
I hope this helps

What a mess...one thing that I forgot to mention that probably would have been astronomically helpful to the community is that I'm taking over someone else's machine. There were lingering settings that just decided they weren't going to play nice.
At the end of the day, I got rid of Postgress.app, installed postgres with Homebrew, and the balance of what I needed was here: http://exponential.io/blog/2015/02/21/install-postgresql-on-mac-os-x-via-brew/
Specifically the line that saved me was createdb 'whoami' (see the actual post...the syntax is a bit different than what I just wrote because of stackoverflow formatting)...in retrospect it seems obvious, but it helped my current logged in user overcome the presets of the other legacy user by actually creating the database that the psql the whole setup was looking for.
Lesson learned!
Thanks for the help #max, helped me avoid another issue I was about to cause as well!

If you are using Postgres.app you should leave out the username and password from config/database.yml. Also that error is telling you that you have entered MyProfile not as the user for (or role in PG parlance) but as the host for the database connection (instead of localhost).
This is really all you need in your config/database.yml to get Postgres.app running:
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: myapp_development
test:
<<: *default
database: myapp_test
Its often a good idea to leave it stripped down and let the fool who absolutely has to have a password on his dev postgres server use the DATABASE_URL env var.

Related

Postgres 13: cannot psql from CMD (incorrect password but its correct?)

Image of my info and the error itself
Yes I can access it by Shell and pgAdmin but I cannot with direct psql command...
Does anyone have any insight?
The thing that confused me was from the CMD I managed to connect as AN USER NAMED "POSTGRES" and connected to a database named "POSTGRES", for a while, I thought users and databases were one thing. Later, I thought that "POSTGRES" is like a master user and I can create many many databases with it then I found out I could create users ...
postgres=#CREATE USER me WITH PASSWORD '0000';
Add to that confusion, I managed to connect as an different user but it was still saying "postgres=#_" not a "differentuser=#" then I figured out that "postgres=#" is the database named "POSTGRES". There weren't any noob friendly tutorials on Youtube that I was able to find... I guess the pgAdmin 4 helped because I wasn't familiar yet with \du+ \l commands. I hope someone finds this useful.

How to create the first DB in Postgres

I have just installed Postgres 12 on a Mac. As you may soon appreciate I am totally new to it.
During the installation process I was asked to provide a "password". I do not remember specifically, but I think it was for the some sort of admin role.
Now I want to create my first database. Reading the documentation I insert the command
createdb myfistdb
the system asks for a password. I give the one I set during the installation processes but I got the following error
createdb: error: could not connect to database template1: FATAL: password authentication failed for user "myusername"
where myusername is the user I am logged in.
The same happens if I give the system password of myusername.
I understand that my question is pretty basic, but I have been struggling quite some time without any success, so any help will be appreciated.
The database uset name used by createdb defaults to the operating system user name, so you'll have to specify the administrative superuser explicitly:
createdb -U postgres myfistdb

Not sure if I have PostgreSQL installed on my Mac properly for creating Flask web app

I installed PosgreSQL 10.5 on a Mac running macOS 10.13.6 using Homebrew by following this tutorial. After installation, I ran =# \du to view all users. I saw the user with my Mac's name that the tutorial says PostgreSQL creates during installation, but the "postgres" default user was not created (or at least did not appear).
My Main Issue is...
I need to login to PostgreSQL 'at the root', and I either do not understand exactly what that means, or something is wrong. In following the very first steps from the PostgreSQL wiki's First Steps documentation, it says to...
First connect/login as root:
# su - postgres
$ psql
psql (9.6.0)
Type "help" for help.
But when I open a terminal window and type in su - postgres it asks for a password. I've typed in every password I could possibly have given that role, and it just says su: Sorry.
I did some research and tried to change the password by doing this:
postgres=# \password postgres.
That prompted me to enter a password twice, and it seemed to work. But, when I go back and run: su - postgres, it just gives me the same su: sorry.
So I tried typing in: psql postgres. This at least changed the $ to postgres=#, which tells me that I am at the PostgreSQL command line, and then the same: su: postgres, but that just changed the (=) sign to a (-), so now it changed to: postgres-#
I thought that maybe this meant that I was "logged into PostgreSQL at the root, but then I ran the following:
postgres=# su - postgres
postgres-# CREATE SCHEMA test
postgres-# CREATE USER tester PASSWORD 'P#ssword1'
postgres-# GRANT ALL ON SCHEMA test TO tester
postgres-# \q
...and none of that worked. It did not create a user or a schema.
History...
I've installed/uninstalled PostgreSQL on this Mac several times
in the past for testing
I've also installed/uninstalled pdAdmin for various reasons
It is highly possible that I did something in all of that shuffle that brought about this issue with the 'postgres' role not being created during this Homebrew instal. So, I did read through the rest of that tutorial and I did type in $ createuser postgres and that did seem to create the 'postgres' role. I tried to use ALTER ROLE to give that role Create DB but I never could get it to work using the command line, so I just opened pgAdmin, saw the role, granted it Create DB, and then back in command line ran =# \du again, and Create DB did appear next to the 'postgres' role I'd created. I added everything in pgAdmin, and now it looks like this.
List of roles
Role name | Attributes | Member of
----------------+------------------------------------------------------------+-----------
MyMacsNameHere | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
postgres | Superuser, Create role, Create DB, Replication | {}
My end goal...
I am trying to follow along with this YouTube tutorial about creating a Flask web application from scratch. In the YouTube tutorial, he uses MySQL, but I want to use PostgreSQL. Needless to say, I am up against a learning curve.

PostgreSQL 9.4, 64-bit windows: Unable to create user from commandline

I'm trying to create a user from command line using PostgreSQL version 9.4 on a 64 bit machine with windows.
I'm using the below command to create a new user:
createuser -d temba
On executing the above command, it prompts me for a password.
I enter the password (qwerty) which i used while installing PostgreSQL. on doing so, i get the below error:
psql: FATAL: password authentication failed for user "my-windows-user-name"
Next, i tried giving my login password for windows, i get the same error as above.
Can anyone guide me through the procedure for creating a new user from command line (only, I'm not allowed to use PgAdmin to create user).
I have checked previous posts with similar errors. But the problem there is, either they are not using windows or they are using a really old version of PostgreSql.
Any information on how to proceed about with this shall be really helpful.
Thanks in advance.
All Postgres command line tools try to connect to the database using the current operating system user if no database user is specified.
So you need to specify the user name of the Postgres superuser that is used to connect to the Postgres server:
createuser -U postgres -d temba
This becomes more evident if you use psql instead. You specify the Postgres user account and the target database when you start it:
psql -U postgres -d temba
Then at the prompt you can run create user ....
temba=# create user foobar ... ;
Which is what the command line tool createuser is doing in the background.

psql: FATAL: database "Morne Nel" does not exist

I need to do a report in JasperReports.... besides the point here.
Ive been given a existing Postgresql DB to use.
I have installed PostgreSQL and all went great. (except the package update at the end. Cant get past the proxy server)
Ive added the C:\Program Files\PostgreSQL\9.0\bin path the the system path as-well.
When I go to cmd and type psql it propts for a password....
I enter password, because thats the only password I added during instalation.
Then is comes up with psql: FATAL: database "Morne Nel" does not exist
step by step hoe do I import this DB into postgres? PLEASE?
When using psql you have to pass database name, otherwise postgres will try to connect to database matching your user name (that is why it tries to connect to "Morne Nel").
You can use createdb console command to create database first. Here is documentation on that command: http://www.postgresql.org/docs/9.3/static/sql-createdatabase.html
But also you can use pg_restore command right away with --create option passed to create and restore database from dump at once (documentation here: http://www.postgresql.org/docs/9.3/static/app-pgrestore.html).
Try to execute those commands (it is possible that you will have to specify your postgres user and password) and let us know what happened. I cannot write more at the moment as I am not sure what dump format do you have so it would be good to provide that information if you won't manage to use pg_restore successfully.