Postgres Rails DB Bug - ruby-on-rails

I have just created a new RoR project on a Godaddy VPS running Ubuntu. The app is set up to run Postgres, which is installed and running.
Wwhen I do the INITIAL rake, I get:
server$ bin/rake db:create db:migrate
FATAL: role "root" does not exist
Run `$ bin/rake db:create db:migrate` to create your database
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:898:in `rescue in connect'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:888:in `connect'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:568:in `initialize'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `new'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/postgresql_adapter.rb:41:in `postgresql_connection'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:435:in `new_connection'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/connection_adapters/abstract/connection_pool.rb:445:in `checkout_new_connection'
/usr/local/rvm/gems/ruby-2.1.2/gems/activerecord-4.1.5/lib/active_record/........
Here is my database yml
development:
adapter: postgresql
encoding: utf8
database: project_development
pool: 5
username: philip
password: ###
test: &TEST
adapter: postgresql
encoding: utf8
database: project_test
pool: 5
username: philip
password: ### - not real
production:
adapter: postgresql
encoding: utf8
database: project_production
pool: 5
username: philip
password: ###
I have tried root and philip.
And I have created roles and users in PG for both.

In Ubuntu, PostgreSQL by default is configured to allow local connections over Unix socket only to authenticate with the same username as the connecting Unix user.
You are running your app as root in your OS. And PostgreSQL forces you to use root as a database user name. If you want it to use a different database account, you need to run your app as a different user.
Apparently, your PostgreSQL server doesn't have a root account.
Ubuntu wiki page about PostgreSQL setup
A dirty fix could be authentication over TCP/IP: set a host to 127.0.0.1 in your database.yml, and these restrictions won't have any effect.
Another way to circumvent it is to edit pg_hba.conf to not require "the same user" for local connections.

You need to create the roles and database by hand:
SSH into the vps and then ...
Switch into the default user:
sudo su – postgres
Once logged in as this user, you can create the new role:
createuser
Enter name of role to add: philip
Shall the new role be a superuser? (y/n) y
To outfit your user with a password, you can add the words –pwprompt to the createuser command:
createuser --pwprompt
Source: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-12-04

I started over by deleting all roles in PG. I recreated what I needed and it is all good

Related

How to fix the "Role doesn't exist" error?

I just created a new Ruby on rails project using this, in order to deploy it with Heroku
rails new -d postgresql LG_1
Then I used rails s to run my server and found this error :
role "esteban" does not exist.
I've been looking for solutions for an hour, but none of them worked. Using createuser just gives me the does-not-exist error, and I'm not very comfortable with Rails.
The versions I'm using :
Ruby 2.4.4
Rails 5.2.3
Pg (gem) 1.1.4
Here is my database.yml file (without the 75 lines of comments) :
default: &default
adapter: postgresql
encoding: unicode
host: /var/run/postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
development:
<<: *default
database: LG_1_development
test:
<<: *default
database: LG_1_test
production:
<<: *default
database: LG_1_production
username: LG_1
password: <%= ENV['LG_1_DATABASE_PASSWORD'] %>
I did not edit this file.
EDIT : I found the solution. I had to open the pg_hba.conf file and manually add a new user called "esteban"
Did you install new Gem for project?
If you did I think what you didn't database migration.
Please run following commands.
$heroku run rake db:migrate
$heroku ps:scale web=1
$heroku ps
$heroku open
I guess esteban is the user name on your local machine and because you dont set specific username and password in database.yml for development mode postgres uses username of your user when trying to open connection to database.
Create role esteban in postgres:
# go to psql console under default postgres user
psql postgres
# create role
create role esteban SUPERUSER LOGIN;
# exit psql
\q
Or set correct one in database.yml if you already have it

ActiveRecord::NoDatabaseError: FATAL: role "postgres" does not exist when I try to migrate bd in rails

I cloned a friend's git repo and I'm trying to migrate the db. I started postgres, but when I run rails db:migrate, I keep getting the errors:
Rails Error: Unable to access log file.
and
ActiveRecord::NoDatabaseError: FATAL: role "postgres" does not exist
I've tried all available solutions online but keep getting the same error. Does anyone know what I'm doing wrong?
Try the following
Setting Up Postgres
Create a Postgres user for the Rails app we'll create in the next step. To do this, switch into the Postgres user:
su - postgres
Then create a user (or a "role", as Postgres calls it):
create role myapp with createdb login password 'password1';
and make sure you have config/database.yml
development:
adapter: postgresql
encoding: unicode
database: myapp_development
pool: 5
username: myapp
password: password1
test:
adapter: postgresql
encoding: unicode
database: myapp_test
pool: 5
username: myapp
password: password1
The solution for me was:
createuser -s postgres
After I ran that on the command line my rake task to create the DB worked

capistrano, rails, PG::ConnectionBad: fe_sendauth: no password supplied

I'm trying to run Capistrano for a rails app with Postgres on Ubuntu 14, I ran into a password error during rake db:migrate-
DEBUG [2823f146] Command: cd /home/ben/apps/mll/releases/20160414014303 && ( export RBENV_ROOT="$HOME/.rbenv" RBENV_VERSION="2.0.0-p645" RAILS_ENV="production" ; $HOME/.rbenv/bin/rbenv exec bundle exec rake db:migrate )
DEBUG [2823f146] rake aborted!
PG::ConnectionBad: fe_sendauth: no password supplied
I tried every solution to similar posts but no luck. For kicks I also tried running just that command in the remote app dir and got the following:
PG::ConnectionBad: FATAL: password authentication failed for user "mll"
FATAL: password authentication failed for user "mll"
This is interesting because it's using my database name as my username. See database.yml below, so for the hell of it I added a mll role, and lo and behold it worked when just running rake db:migrate. I tried running Capistrano again though with this new role, and still no luck.
Is it a reasonable guess that the username is not being accessed/stored properly? Any way for me to test that? I manually ALTER ROLE ben WITH PASSWORD 'mypw'; for both my ben and mll roles, and nothing.
My database.yml:
defaults: &default
adapter: sqlite3
encoding: utf8
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/development.sqlite3_test
production:
<<: *default
host: localhost
adapter: postgresql
encoding: utf8
database: mll
pool: 5
username: <%= ENV['DATABASE_USER'] %>
password: <%= ENV['DATABASE_PASSWORD'] %>
\du:
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
ben | Superuser, Create role, Create DB | {}
mll | Superuser, Create role, Create DB | {}
postgres | Superuser, Create role, Create DB, Replication | {}
I read changing md5 to trust helped some people, I tried that, but I'm unsure how to restart, all commands I've seen haven't worked for me.
pg_hba.conf:
local all postgres peer
# TYPE DATABASE USER ADDRESS METHOD
local all all peer
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
You should use "trust" under method for localhost in pg_hba.conf. Note that this means all connections from localhost will be able to login as any user, which is probably fine as long as you're using this for development.
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
After changing your pg_hba.conf you can restart postgres with pg_ctl reload
The best practice to resolve the issue is -
Add the below gem in your Gemfile under development group and bundle install.
gem 'capistrano-postgresql'
Add the below line in Capfile
require 'capistrano/postgresql'
Add below lines in config/deploy.rb or in config/deploy/*.rb
set :pg_password, ENV['DATABASE_PASSWORD']
set :pg_ask_for_password, true

Rails error creating database with postgres

the first time when postgresql is installed in my local machine i created a user "user1" using the following command :
$ sudo su postgres -c psql
postgres=# CREATE ROLE user1 SUPERUSER LOGIN;
postgres=# \q
then setup my database.yml
development:
adapter: postgresql
encoding: unicode
database: appname_development
pool: 5
timeout: 5000
username: user1
password:
and all work fine, so today i created a new app then i did the same steps (creating a new user "user2" and modify my database.yml)
development:
adapter: postgresql
encoding: unicode
database: application2_development
host: localhost
pool: 5
timeout: 5000
username: user2
password:
but when i try to create the database
$ rake db:create:all
i get this error :
FATAL: Peer authentication failed for user "application2"
i tried to fix the problem by adding host: localhost to my database.yml but now i get another error which is :
fe_sendauth: no password supplied
can someone explains to me what means this error, and why the first time when i created the first user it doesn't show me this error ? then i will be thankful to know the solution as well.
thank you
I have already faced this problem and fixed it.
I think, It will be helpful to you.
please find 'pg_hba.conf' and postgresql.conf files.
generally, pg_hba.conf and postgresql.conf files located at /etc/postgresql/9.1/main/.
put `local all all md5' in pg_hba.conf and save it.
open postgresql.conf file and if listen_addresses = 'localhost' and port = 5432 line are commented. then uncomment to it.
Finally restart to pg server and try to create database.

Working on a rails app locally with a remote Postgres connection?

Is there a way to configure the database.yml file to connect to Heroku's Postgres remotely?
I'm having trouble understanding how Heroku, Rails and PG gem work together.
It looks like during deployment, Heroku re-writes the database.yml file - is it possible to see the contents of this updated .yml file and use it locally?
Below are the steps to access Heroku db from local development:
Login to your heroku account.
Navigate to this URL https://postgres.heroku.com/databases/ OR you can get heroku db url by running this command in the terminal: heroku pg:credentials:url
Select your application database.
You will able to see your heroku pg credentials:
Host xxxxxxxxx.77777.amazonaws.com
Database 42feddfddeee
Username 44444444444
Port xxxx
Password 777sfsadferwefsdferwefsdf
collect above details and put in your databse.yml file development evn:
adapter: postgresql
host: < host > # HOST
port: < port > # Port
database: < database name > # Database Name
username: < user_name > # User Name
password: '< password >' # Password
Restart you application,
Best of luck..!!
I am not a postgres user but this should work.
You can alter your database.yml to include host and port
production:
adapter: postgresql
encoding: unicode
database: di_production
pool: 5
username: user
password:
host: heroku.host.name
port: <postgres listen port>
And of course, you should allow connection on the server side, both at the firewall level and database level.
A simple hack to see contents of #{Rails.root}/config/database.yml is to write code to load this yml into an object, then print it out on the UI
DB_CONFIG = YAML.load(File.read("#{Rails.root}/config/database.yml", __FILE__))
puts DB_CONFIG["production"].inspect # or what ever method you want to print it
I am a bit of a newbie and may have misunderstood your question - but. Developed a ROR application using postgress database. Then uploaded to Heroku. I ran DB/Migrate/schema.rb to set up the remote Postgresql database.
From memory heroku run rake db:init (but I could be wrong). Whenver I update the database in develpment to get update in Heroku I have to promote code and run heroku run rake db:migrate
From my config/dtabase.yml
development:
adapter: postgresql
encoding: unicode
database: di_development
pool: 5
username: davidlee
password:
test:
adapter: postgresql
encoding: unicode
database: di_test
pool: 5
username: davidlee
password:
production:
adapter: postgresql
encoding: unicode
database: di_production
pool: 5
username: user
password:
and it works. I can't remember doing anything else.
Pierre

Resources