Rails - connecting to postgres with url, role does not exist - ruby-on-rails

I'm attempting to deploy a Rails 5 app using Capistrano, and I'm hung up on the db:migrate step.
Specifically, when my scripts attempt to run db:migrate I get the error:
ActiveRecord::NoDatabaseError: FATAL: role "admin" does not exist
My database.yml file looks like this for the production block:
production:
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 } %>
url: <%= ENV['DATABASE_URL'] %>
and my database_url takes the form of:
postgresql:://mydb_user:mydbuser_pass#localhost/db_name
With mydb_user, mydbuser_pass, and db_name substituted in with my own correct values.
Inside of postgres I can see these permissions on my database:
db_name | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =Tc/postgres + postgres=CTc/postgres + mydb_user=CTc/postgres
Which I think means that mydb_user has the permissions necessary for using my database.
Now, the user I'm logged in as during the capistrano deploy does have the username of 'admin', which leads me to believe that I fundamentally don't understand something about postgres roles. Why would my currently logged in user have anything to do with the postgres connection when I've given my username and password via the database url for a specific postgres user I've already created? Is this saying that my ubuntu username needs to match mydb_user in order to login? How do I fix this error specific to rails and database urls with postgres?

prev commenter was wrong: correct format of database url is
postgresql://[user[:password]#][netloc][:port][/dbname][?param1=value1&...]
for example:
postgresql://db_user:db_password#localhost:5432/db_name?timeout=5000&connect_timeout=5&pool=10

Classic case of dumb mistakes. The urls do not have two colons in them like I describe above, instead they are
postgres://
By default rails was attempting to connect as 'admin' because that's the unix user I was logged in as. Solution, double check your postgres url format.

Related

How to configure database.yml for production

I would like to deploy my application via Dokku to a VPS.
Putting together the dokku-postgres documentation and the relative, scarce internet documentation on the matter (one at GitHub), it seems necessary to configure database.yml to use the url environment variable url: <%= ENV['DATABASE_URL'] %>
Since I could not find any other sources of information, I wonder how database.yml should be configured, and how Rails will connect to the postgres service created with Dokku.
For instance, taken for granted that linking url to the DATABASE_URL variable is necessary, will this be enough to establish a connection between my Rails application and the postgres service or would it be still necessary to use a username and a password? In the latter case, what username and password am I expected to use?
Below is how at present my database.yml looks like.
default: &default
adapter: postgresql
encoding: unicode
pool: 5
username: asarluhi
development:
<<: *default
database: fireworks_app_development
test:
<<: *default
database: fireworks_app_test
production:
<<: *default
database: fireworks_app_production
pool: 25
username: fireworks_app
password: <%= ENV['FIREWORKS_APP_DATABASE_PASSWORD'] %>
This file was created as it is (apart from a higher pool size for production) when I created the application. How would you suggest to edit the production section?
The dokku-postgres documentation states that the following (and nothing else) will be set on the linked application by default:
DATABASE_URL=postgres://postgres:SOME_PASSWORD#dokku-postgres-lolipop:5432/lolipop
In place of the lollipop postgres service example, I would use fireworks_app_production to match the name of the database in database.yml
Are username and password still necessary after pointing url to the DATABASE_URL variable? Am I expected to add or remove anything else?
You don't need to worry about the database.yml with dokku, just upload your app to the server, let's use "fireworks" as the name for example on this.
when you upload the first time the app, this is created automatically so you don't need to create it.
then you install the postgres plugin and run
# that will create the container for the database
$ dokku postgres:create fireworks
# and then you link both, the app with the database
$ dokku postgres:link fireworks fireworks
you don't have to worry about anything else, with that dokku will connect this
then you just have to run db:migrate and everything is ready to work!

Rails: permission denied for relation schema_migrations

I'm trying to setup a local production environment for a Ruby on Rails web application. I can run the application with rails server command, which gives the development environment.
The production environment I'm trying to set up is purely local and I've followed this tutorial for setting it up with apache 2: https://www.digitalocean.com/community/tutorials/how-to-setup-a-rails-4-app-with-apache-and-passenger-on-centos-6
However when I go to the page of my application I get the following error:
PG::InsufficientPrivilege: ERROR: permission denied for relation schema_migrations : SELECT "schema_migrations".* FROM "schema_migrations"
in my database.yml I have these settings for development and production:
adapter: postgresql
database: whiteboard
username:
password:
pool: 5
timeout: 5000
I'm not allowed to change these settings, no matter what.
Is there any way to fix this? (if yes, step by step please)
It seems you have to create a DB user with all needed privileges on your DB.
For example I think you could do the trick by log in your DB console then do something like:
CREATE USER your_new_username WITH PASSWORD 'your_new_password';
CREATE DATABASE whiteboard;
GRANT ALL PRIVILEGES ON DATABASE whiteboard to your_new_username;
ALTER DATABASE whiteboard OWNER TO your_new_username;
Then update you database.yml like this:
adapter: postgresql
database: whiteboard
username: your_new_username
password: your_new_password
pool: 5
timeout: 5000
Hope it helps!
I was using dbmate which also creates a table called schema_migrations on startup, and thus fails when a full dump is applied that also has the same table. Here are a few approaches
PostgreSQL doesn't support creating dump with IF NOT EXISTS, unlike mysql:
See Can pg_dump be instructed to create tables with "IF NOT EXISTS"?
However, PostgreSQL 9.1 and newer supports CREATE TABLE IF NOT EXISTS -syntax, so you could string replace the dump for example by using sed:
sed -i 's/CREATE TABLE/CREATE TABLE IF NOT EXISTS/g' dump.sql
But in practice, I ended to delete the lines from the dump with schema_migrations on it, and +1-2 lines following:
sed -i '/CREATE TABLE public.schema_migrations (/,+2 d' dump.sql
sed -i '/ALTER TABLE ONLY public.schema_migrations/,+1 d' dump.sql

Mysql2::Error Access denied for user

I have some rails applications on my mac and they work just fine.
I have just installed a new one that uses mysql2 gem, I am starting
the server but when I am visiting the application I am getting an
Access denied error.
I am trying to follow this solution
Mysql2::Error: Access denied for user 'test'#'localhost' to database 'depot_test'
I am logging in as a root user but when I am trying to create a new database I am getting an access denied error.
Changes in your config / database.yml the user name root. test user does not exist in MySQL. I think in your development environment should look like:
Example
development:
adapter: mysql2
database: depot_test
pool: 5
host: localhost
encoding: utf8
username: test
password:
Change username to root or create test user with privileges. Remember that Rails uses environments(test, development, production u other). In your case is depot_development or another name that is not equal to another environment.
I hope it helps you.

Teach Rails 3 to talk to Amazon Redshift - permission denied to set parameter "client_min_messages"

Background
I just spun up a new Redshift instance on AWS and I can connect to it via the psql cli client without a problem.
The Problem
I'm trying to get my Rails 3 app to connect to the Redshift box. I have the pg gem and the correct adapters. My database.yml file has the following details:
redshift_development:
adapter: postgresql
encoding: utf8
database: db_name
port: 5439
pool: 5
schema_search_path: schema_name
username: my_uname
password: password
host: host
I manually created a table on the box called cellphones, and a model in my app app/models/cellphone.rb:
class Cellphone < ActiveRecord::Base
establish_connection 'redshift_development'
end
When I try to load in the app in the rails console and call Cellphone, I get the following error message:
[1] pry(main)> Cellphone
PG::InsufficientPrivilege: ERROR: must be superuser to examine "client_min_messages"
: SHOW client_min_messages
PG::InsufficientPrivilege: ERROR: permission denied to set parameter "client_min_messages" to ""
: SET client_min_messages TO ''
=> #<Devise::Models:0x3feef95050b4>
The Really Weird Part
I'm already logged in as the superuser.
psql=# \c
psql (9.4.0, server 8.0.2)
SSL connection (<unimportant stuff>)
You are now connected to database "db_name" as user "my_uname".
psql=# \du
List of roles
Role name | Attributes | Member of
-----------+-------------------------------+-----------
rdsdb | Superuser, Create DB +|
| Password valid until infinity |
my_uname | Superuser, Create DB |
How can I fix this?
Optional Info
This issue occurs because Redshift is a heavily customized fork of an old PostgreSQL version. No user is allowed to call show client_min_messages. The version of activerecord that ships with Rails 3.2.14 tries to call this function under the hood (along with a few other functions disallowed by Redshift).
I would be very surprised to find that AWS doesn't have an official workaround for Rails apps. At the moment I'm working with a version of the activerecord Postgres adapter that I modified to remove the offending code - but I would really rather not use this in production.
There are redshift adapters for Rails, so it's worth to try them first.
https://github.com/fiksu/activerecord-redshift-adapter for Rails3
https://rubygems.org/gems/activerecord-redshift-adapter
https://github.com/aamine/activerecord4-redshift-adapter for Rails4

Database not being selected in rails project when attempting to rake db:migrate

Working with a rails app, having some manner of weird database / rake issues.
When I execute:
rake db:migrate
I am getting the following error:
Mysql2::Error: No database selected: SHOW TABLES
(See full trace by running task with --trace)
The trace isn't revealing much useful information. Can be seen here: http://pastebin.com/WdsguudC
The config file looks right, and the user is getting logged in, or I would have gotten some kind of access error. The database exists, the user has correct permission, and I can access and manipulate it manually. I have done a bunch of googling, and haven't found anything helpful. Not sure if there is any other code that needs provided, because this seems like fairly low level problem.
after all that it was a spacing issue in the yaml.
Note that ruby has exchanged its YAML parser in a recent 1.9.2 version.
This might also cause this problem.
In order to switch back to the old YAML parser syck, use this in boot.rb:
require 'yaml'
YAML::ENGINE.yamler= 'syck'
Well, it is a common issue for us beginners. This issue comes from the moment when you create your new project in rails. Let’s say to have an example
$ rails new toy –d mysql
After you do the bundle and start your server, most likely you will have an error. To correct it you need to go to your database.yml and modify the following:
Add a password in the password field as shown below, this is the password you use to secure mysql.
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: mypassword
socket: /tmp/mysql.sock
Also, comment out the database adding a hash tag (#)before the name as shown below
development:
: *default
database: #toy_development
Then restart your command line and go to the root of your application and type:
$ rails s
You have to see the Ruby on Rails welcome page..
After, you need to create a database.
Create a DATABASE.
The issue message is saying that not DATABASE is selected. It is because I didn’t create one. When you work with MySQL you have to create one, so:
Go to the root of my application and type:
$ mysql –u root –p
$ Passwor: mypassword (Enter your password, this is the one you entered to secure MySQL)
Note: This example works wit a project called toy and the user I wanted to grant privileges is mark and the password I’ll give is 45mark. Below you will see where I apply these elements. Remember to apply your own elements on each part of the statement.
Create and user for this project
Once you are in, you will see the pointer (mysql> ), so type after it:
mysql> GRANT ALL PRIVILEGES ON toy_development.* TO 'mark'#'localhost' IDENTIFIED BY '45mark';
Then type:
mysql> exit;
Check that it is working by typing:
$ mysql –u mark –p toy_development
Enter password: 45mark (You enter the one you gave)
Open database.yml file and configure what is needed and fix as required. In my case I will chance the username to mark and the password to 45mark
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: mark
password: 45mark
socket: /tmp/mysql.sock
- Also, REMOVE the hash tag (#) added before
development:
: *default
database: toy_development
Save it.
Go to the root of the application and type
$ rake db:schema:dump
Done!!
I hope this helps. Happy coding!!
Thanks
I had the same issue with ruby 1.9.2-p180 , upgraded to p290 and it works
Just restart the server; in the command line:
Press Ctrl + C
execute:
rails s
I had a similar error when i typed rake db:schema:dump and it turns out that I just have to comment out all the databases on my yaml file except my development one.
Give a try to this.
rake db:test:prepare
Install this to see if you have actually created a table or not. Open the "development.sqlite3" in db folder
http://sqlitebrowser.org/
Its a simple error checkout the entire database.yml file and see that where is default decription is given database name is given or not if not then look below it there will another development name is also given where configuration of database is use check that give your database name in it
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: 12345
host: localhost
development:
<<: *default
database: db_name
One potential cause is that there is a DATABASE_URL environment variable defined.
$ echo $DATABASE_URL
=> mysql2://root#localhost:3306
If you get a similar output to the above url (i.e., the database name is not present), then you might want to add the database name to the string or unset the env var.
$ export DATABASE_URL=mysql2://root#localhost:3306/my_rails_app_development
$ unset DATABASE_URL
If you unset the var, you probably want to specify the database details in database.yml instead.

Resources