Getting this error:
PG::InsufficientPrivilege: ERROR: permission denied to set parameter
"client_min_messages" to "warning" : SET client_min_messages TO
'warning' when connecting with redshift database.
enter image description here
My database.yml file setting looks like this
development:
adapter: postgresql
encoding: utf8
host: nacfhrcluster123.ctvpledrvuobs5.us-east-1.redshift.amazonaws.com
port: 5439
username: nacfhr123
password: NACFDChr12345!
database: devnacfhrdc
pool: 5
schema_search_path: 'beta'
timeout: 5000
min_messages: warning
Thank you guys for your responses. I resolved the issue by adding a gem with the name 'activerecord5-redshift-adapter' in my gemfile. So you have to add below line in your gem file.
gem 'activerecord5-redshift-adapter'
And then run bundle install again.
It is important to realise that Redshift IS NOT Postgres. There are many differences.
One difference is that the available parameters are much different, the only parameters that can be set on Redshift are:
analyze_threshold_percent
datestyle
extra_float_digits
query_group
search_path
statement_timeout
wlm_query_slot_count
You will need to alter your rails connection so that is does not request this parameter to be set.
I guess that means removing min_messages: warning
I hope this will help. As far as I know there is no active records available for Redshift and it don't it make sense as well as Redshift is not a typical RDBMS.
We have similar scenario where we use ROR for front end for one of analytics and planing application.
Here is our app scenario:
Front-end : ROR
Database : mysql(OLTP)
Datawarehouse : Redshift(OLAP)
This is how data flows back and forth between ROR and Redshift.
Insert/updates from ROR to Redshift(happens very rarely).
For any operations from ROR that impacts something on data warehouse, ROR executes PSQL commands with specific query, not via active records.
SELECT from Redshift(happens frequently)
Similarly, for getting data from DW for various reason done via select query and redirection to file using PSQL command, then it import it into OLTP using myssqlimport.
Link for psql, mysqlimport tools that we use.
Comment if you have further specific followup questions.
Related
I have to connect to a SQL Server database that I do not control. The name is something like "foo.bar-baz.aaa". I've configured the name in config/database.yml with database: foo.bar-baz.aaa. When I attempt to query any model in that database, rails complains that "Database 'foo' does not exist".
I'm figuring that somewhere in the query builder, that period between "foo", and "bar-baz" is causing some mayhem. Is there any way that I can work around this?
The answer turns out to be quoting the DB name, while also "delimit identifying" the name (as per Larnu's comment).
Now, my config/database.yml file has this configuration:
database: '[foo.bar-baz.aaa]'
Thanks to both BenFenner, and Larnu for pointing me in the right direction.
I have devise gem in my application. I've been playing around with making some tables etc. Now I want to separate the database for the admin and the database for the user. I know its one single database. But, I'm not sure how to get it going in Rails.
you can configure multiple databases in database.yml file
production:
primary:
database: my_primary_database
user: root
adapter: mysql
secondary:
database: my_secondary_database
user: secondary_root
adapter: mysql
migrations_paths: db/secondary_migrate
and then in your modal, you can mention which database to use
class AnimalsBase < ApplicationRecord
self.abstract_class = true
connects_to database: { writing: :secondary }
end
checkout this link for more detail https://edgeguides.rubyonrails.org/active_record_multiple_databases.html
**For rails 4.2 - 5 ** you can use this gem Multiverse
P.S: Rails 6 is coming with a more neat solution for this, a stable build for rails 6 is now available you can upgrade to newer version too.
Rails have some limitation in which connecting only single database is major one. So I used to switch database using configuration setup in database.yml where you will have development & admin_development, (test & production etc. also)
You can get following constants,
ADMINCONFIG = YAML.load_file("#{Rails.root}/config/database.yml")['admin_development']
DEVCONFIG = YAML.load_file("#{Rails.root}/config/database.yml")['development']
And later whenever you need, you can switch from one to another as per requirement (through controller),
ActiveRecord::Base.connection(ADMINCONFIG)
I have a connection to an external database (In addition to the rails database).
I want to be able to show the connection information except for the password in a view.
ActiveRecord::Base.connection_config works fine and shows the connection information for the rails db connection.
However, my external table is
customer_tables:
adapter: sqlserver1
host: sqltest1
port: 1440
database: CUSTOMER
username: xxx
password: xxx
schema_search_path: dbo
How do I get the info for that connection/db to show as it does for the default rails db?
I dug around a bit more and found an answer at
Stack overflow post
The solutions is to put <%= CustomerTable.connection_config %> in the view.
I'm on the early stages on learning Ruby.I really don't have an idea on how to use an existing database filled with tables and data on ruby. Every guide, every article that I have or find on the internet is always creating a new one using the migration functions.
But which are the steps for using an existing database in SQL server on RoR?
You're in luck, friend. My first Rails project (7 years ago) was against a horribly set up SQL Server database.
Per the above, you need to set up your database.yml appropriately. But for an existing database, obviously it is unlikely that the table and column names conform to the Rails conventions. The good news is that you can override all of those defaults. Here is a non-exhaustive list of those directives:
In a model descended from AR::Base,
set_table_name 'actual_table_name'
set_primary_key 'actual_primary_key_name'
On the various association directives (has_one, has_many, belongs_to), there are :foreign_key keys that let you specify the name of the foreign keys.
Now, one of the things that MS SQL Server allows you to do which is TERRIBLE is that you can embed spaces into your column names. Fear not, you can still refer to these columns by name using write_attribute("badly named column") and read_attribute("badly named column"). You may also refer to them in various directives like so:
validates_length_of "Fax Number", :maximum => 17, :allow_nil => true
Finally, you may refer to the implied methods these devilishly named columns generate like so:
self.send('Fax Number=', new_fax_number)
Obviously, you can't refer to them as symbols, since spaces are disallowed in Ruby symbols.
Good luck, and next time I hope that you get to work with a real RDBMS, like Informix :).
First you have to setup your application to user sql server for databases connectivity.
you have to use gem for sql server in your gemfile and have to setup database.yml file accordingly.
In database.yml, in config folder put the name of same database In the Development part of this file.
development:
adapter:
database: db_name_dev
username:
password:
host: localhost
socket:
To use a existing server. In your database.yml you have to specify the host, port and the database name.
`database: <host>:<port>/<database_name>`
For eg
development:
adapter: mysql2
database: your.mysqlserver.com:1521/project_database
username: project_user
password: project_password
I'm refactoring some features of a legacy php application which uses multiple databases with the same structure, one per language. When an user login choose his language and then all the following connections are made with the db for that app with some code like this:
$db_name = 'db_appname_' . $_SESSION['language'];
mysql_connect(...);
mysql_select_db($db_name);
I'd like to refactor also the database, but currently it's not an option because other pieces of software should remain in production with the old structure while the new app is developed, and for some time after it's been developed.
I saw this question, but both the question and the suggested gems are pretty old and it seems that they are not working with Rails 3.
Which is the best method to achieve this behavior in my new rails 3 app? Is there any other choice that avoid me to alter the db structure and fits my needs?
Last detail: in the php app even the login information are kept in separate tables, i.e. every db has its own users table and when the user logs in it also passes a language param in the login form. I'd like to use devise for auth in the new app which likely won't work with this approach, so I'm thinking to duplicate (I know, this is not DRY) login information in a separate User model with a language attribute and a separate db shared among languages to use devise features with my app. Will this cause any issue?
EDIT:
For completeness, I ended with this yml configuration file
production: &production
adapter: mysql
host: localhost
username: user
password: secret
timeout: 5000
production_italian:
<<: *production
database: db_app_ita
production_english:
<<: *production
database: db_app_eng
and with this config in base model (actually not exactly this but this is for keeping things clear)
MyModel < AR::Base
establish_connection "production_#{session[:language]}"
...
end
use establish_connection in your models:
MyModel < AR::Base
establish_connection "db_appname_#{session[:language]}"
...
end
Use MultiConfig gem I created to make this easy.
You could specify the configs in separate file like database_italian.yml etc and then call:
ActiveRecord::Base.config_file = 'database_italian'
This way it will be much easier to maintain and cleaner looking. Just add more language db config files as you wish