rails with postgresl database "role postgres does not exist" - ruby-on-rails

I installed postgres in my rails application, but I cannot create any roles.
For su - postgres and psql -d template1 -U postgres I get psql:FATAL role "postgres" does exist
I also ran createuser -s -U $USER
What am I doing wrong?
Thanks!

Here's a list of already answered questions:
psql: FATAL: role "postgres" does not exist (with -h localhost option)
psql: FATAL: role "postgres" does not exist
psql: FATAL: database "<user>" does not exist
Depending on how you installed PostgeSQL, you may not have the role postgres. For instance, on my machine, when I install PostgreSQL via Homebrew, the install creates the default user as the current logged in user, not postgres.
Therefore, to login I must use
psql -U <myuser> -h localhost
and not
psql -U postgres -h localhost
You may get the error
psql: FATAL: database "<user>" does not exist
In that case, follow the instructions in this topic to create the user database.

Related

Problem with user authentication via psql

I am relatively new to db administration in postgre, and I am loosing tons of time trying to figure out what I am doing wrong here. My goal is to create a user, a db, and to connect to that db from under that user.
What I am doing for that is the following:
sudo -u postgres psql
postgres=# create database mydb;
postgres=# create user anna with encrypted password 'anna';
postgres=# grant all privileges on database mydb to anna;
Here is all sucess.
Then I try to connect to db this way:
sudo -u anna psql mydb
And I end up in a strange error:
sudo: unknown user: anna
sudo: unable to initialize policy plugin
The -u option of sudo specifies an operating system user, not a database user.
Instead, use the -U option of psql:
sudo -u postgres psql -U anna -d mydb
This will not work if pg_hba.conf is configured for peer authentication on local connections, unless you specify an additional name mapping.

I can connect to server's postgres with Rails, but am unable to through the command line

I'm trying to connect to a server's Postgres db through a script. I've ssh-ed into the box and tried
$ psql postgres -U my_username -W #And then entered the password
and get the error:
psql: FATAL: Peer authentication failed for user "my_username"
However, the Rails application running on this server is using these same credentials. I've used ActiveRecord::Base.connection_config in the rails console and used the credentials exactly as they are when trying to use psql. In the rails console, I'm able to query on the database, so I do know the connection is working there.
Is it possible there's a restriction somewhere that's preventing me from connecting through psql? Or is there something else I'm doing wrong?
Try to connect like this:
psql postgres -U my_username -h 127.0.0.1 -W #And then entered the password
Have you tried it like this:
psql --username="postgres"
Another possibility would be to inform more parameters
psql --username="postgres" --host="<ip or host name>" --port="<port, default 5432>" -W <db name>
Good look!

Configuring Username and Password in PostgreSQL

I have just reset my password in psql by editing my pg_hba.conf file to
local all all trust
before changing it back to
local all all md5
after. After doing this I tried to restart the psql server but I found that I have no access to any the server through the pg_ctl commands, for example if I try:
pg_ctl -D /Library/PostgreSQL/9.6/data restart
The reply is:
pg_ctl: could not open PID file
"/Library/PostgreSQL/9.6/data/postmaster.pid": Permission denied
The only way that I'm able to open psql is with this command:
psql -U postgres
Because if I try just 'psql', the response is the following:
psql: FATAL: role "robertosullivan" does not exist
I need to find some way of connecting to psql with my username and my new password so that I can use psql with my app in Rails.
Any advice would be much appreciated, thanks.
First of all, "robertosullivan" does not exist. Which means that you have configured your PG with a user account that simply isn't there. Seems like the easiest way around this would be to create the user like so:
sudo -u postgres createuser robertosullivan -s
sudo -u postgres psql
postgres=# \password your-password
Below query might help you
psql -h localhost -U postgres -d postgres

Unable to locate PostgreSQL folder

I'm in the process of trying to find out or reset my PostgreSQL password, which is unknown to me, but which I need in order to migrate my database from SQLite3 to PostgreSQL.
I was trying to locate the hba_conf file so in the terminal I entered:
ps aux | grep postgres
and I found that the directory I needed to find was:
/Library/PostgreSQL/9.6/bin/postmaster -D/Library/PostgreSQL/9.6/data
My problem now is that it is not possible to locate this file because it apparently doesn't exist! When I cd to Library I'm unable to go any further because there is no PostgreSQL folder listed.
This is a bit of a dead end for me as I have no idea why PostgreSQL is not there. PSQL came with my version of Rails, and I updated it. When I type: 'psql -V' in the terminal, the answer is 'psql (PostgreSQL) 9.6.3'.
Help would be much appreciated, thanks :-)
From the library folder, if I run 'sudo su' then enter ls, I get the following:
.localized Calendars Dictionaries Internet Plug-Ins
Maps Saved Application State WebKit
Accounts CallServices Favorites Keyboard
Messages Screen Savers com.apple.nsurlsessiond
Address Book Plug-Ins ColorPickers FontCollections
Keyboard Layouts Metadata Services iMovie
Application Scripts Colors Fonts KeyboardServices
Passes Sharing
Application Support Compositions GameKit Keychains
PreferencePanes Sounds
Assistant Containers Google LanguageModeling
Preferences Spelling
Assistants Cookies Group Containers LaunchAgents
Printers Suggestions
Audio CoreData IdentityServices Logs
PubSub SyncedPreferences
Caches CoreFollowUp Input Methods Mail
Safari Voices
and if I enter ps I get this:
PID TTY TIME CMD
359 ttys000 0:00.02 login -pfl robertosullivan /bin/bash -c exec -
la bash /bin/bash
3267 ttys000 0:00.02 sudo su
3269 ttys000 0:00.01 su
3270 ttys000 0:00.00 sh
3271 ttys000 0:00.00 ps
If I try 'sudo find / -name psql' - I get:
find: /dev/fd/Library: No such file or directory
find: /dev/fd/Library: No such file or directory
/Library/PostgreSQL/9.6/bin/psql
/usr/local/bin/psql
/usr/local/Cellar/postgresql/9.6.3/bin/psql
When I try 'sudo find /Library/PostgreSQL/9.6/data -name *.conf' I get:
/Library/PostgreSQL/9.6/data/pg_hba.conf
/Library/PostgreSQL/9.6/data/pg_ident.conf
/Library/PostgreSQL/9.6/data/postgresql.auto.conf
/Library/PostgreSQL/9.6/data/postgresql.conf
The installation procedure creates a user account called postgres that is associated with the default Postgres role. In order to use Postgres, we can log into that account.
You can run the command you'd like with the postgres account directly with sudo
sudo -u postgres psql
This will prompt for the password for the postgres user.
If you don't have the password for this postgres user, follow the below steps:
sudo vim /etc/postgresql/9.3/main/pg_hba.conf
Around the line number 84,85 change that to
# Database administrative login by Unix domain socket
local all all trust
then Restart the PostgreSQL service via SUDO command
sudo /etc/init.d/postgresql restart
Now You can run the command to log in to the postgres account directly with sudo
sudo -u postgres psql
You will be now entered and will See the Postgresql terminal.Once you have successfully logged into postgres, you can change the password by the command
\password
and enter the NEW Password for Postgres default user, After Successfully changing the Password again go to the pg_hba.conf and revert the change to "md5"
Around the line number 84,85 change that now to
# Database administrative login by Unix domain socket
local all all md5
then Restart the PostgreSQL service via SUDO command
sudo /etc/init.d/postgresql restart
now you will be logged in as
psql -U postgres
with your new Password.
Please let me know if you have any Issues.
as we identified your data_dir as /Library/PostgreSQL/9.6/data/ and found configuration files in it, as we identified your postgres cluster is running with pg_ctl: server is running (PID: 88) /Library/PostgreSQL/9.6/bin/postgres "-D/Library/PostgreSQL/9.6/data, in order to reset your password do:
become a postgres user: sudo su - postgres
login locally with psql
reset the password with alter user USERNAME password 'NEW_PASSWORD'
after that you can connect as that user with psql -U USERNAME -h localhost using your new password

authentication error for PostgreSQL user?

i am trying to run rake db:create db:migrate db:seed inside a ruby on rails project, i get an error saying FATAL: password authentication failed for user "postgres"
Looking at other similar questions, a lot of people pointed out the validity bug in the pg_hba file, but as far as i can tell, there is no issue with it?
local all postgres md5
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
I also ran into this sudo -u postgres psql -x -c "select * from pg_user where
usename='pand got the following:
-[ RECORD 1 ]---------
usename | postgres
usesysid | 10
usecreatedb | t
usesuper | t
usecatupd | t
userepl | t
passwd | ********
valuntil |
useconfig |
more info : System Ubuntu 14.04 running in a vagrant box, with postgres v9.3.
update: I created a new database and a new user following the instructions found hereenter link description here , Added the new info to the database.yml file found in the config directory just to be sure I am using the DB user no the system user.
update-2: here is the info found in the database.yml file development:
development:
adapter: postgresql
encoding: unicode
host: localhost
database: db1
pool: 5
username: dev1
password: dev1
test:
adapter: postgresql
encoding: unicode
host: localhost
port: 5432
database: db_test1
pool: 5
username: dev1
password: dev1
As there could be quite a number of factors, it could be difficult to point out what is wrong in your setup. I guess I will just try to narrate how I use vagrant + rails + postgresql so I hope this helps you. This assumes that you have a fresh vagrant box with Ubuntu 14.04.
Let's say you have installed the necessary packages for ruby + rails. And now you are going to install postgresql.
sudo apt-get install postgresql postgresql-contrib libpq-dev
And then you would login to your postgresql admin user:
sudo su postgres
psql
Once logged in to the psql console, I would usually create a user named vagrant. I do this because postgresql defaults to using the current shell user as the database user when logging to the db console. This is more of a convenience(for me anyway). And then I would create the database in the console too. After that I give privileges for the database to the user vagrant.
CREATE USER vagrant WITH PASSWORD 'password';
CREATE DATABASE db1;
GRANT ALL PRIVILEGES ON DATABASE db1 to vagrant;
After these steps, I would just add the appropriate database.yml config and run rake db:migrate. I find these workflow effective for me and I never encountered any permission problems with this. I also never touched pg_hba.conf with this setup. Please try this out and I hope this resolves your issues.
Cheers!

Resources