Unable to create database with ActiveRecord from script - ruby

The commands I run:
config['development']
=> {"adapter"=>"mysql2", "encoding"=>"utf8mb4", "collation"=>"utf8mb4_bin", "pool"=>1, "username"=>"root", "password"=>nil, "host"=>"localhost", "database"=>"my_db"}
ActiveRecord::Base.establish_connection(config['development'])
=> #<ActiveRecord::ConnectionAdapters::ConnectionPool:0x007fe20592b348 ...
ActiveRecord::Base.connection.create_database(config['development']['database'])
ActiveRecord::NoDatabaseError: Unknown database 'my_db'
It says unknown database but I am actually trying to create the database with create_database and I follow the docs:
https://apidock.com/rails/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter/create_database
Why is it not working?

Given database.yml:
database.yml
default: &default
adapter: mysql2
encoding: utf8mb4
collation: utf8mb4_bin
pool: 5
username: root
password:
host: localhost
development:
<<: *default
database: my_db
test:
<<: *default
database: my_db_test
I got it working by establishing a connection without the database and then creating the database:
#config = YAML.load(File.open(File.expand_path('../../lib/my_gem_config/database.yml', __FILE__)))
ActiveRecord::Base.establish_connection(config['default'])
ActiveRecord::Base.connection.create_database(#config['development']['database'])

When creating a database in postgres it will use a "template" database to make a copy. I haven't read the source code on how ActiveRecord::Base.connection.create_database actually works and if it indeed does the same. I was able to create the database with your code except that for my config hash I merged: database: 'postgres', schema_search_path: 'public' and that seemed to do the trick. In my case 'postgres' is my template database.

Related

ruby sequel in sinatra framework add postgres schema search path

I have a sinatra service that reads from postgres and we changed the postgres schema of those models. How can I set the schema path.
In rails and active record you can do as follows:
performance_tracking:
adapter: postgresql
encoding: utf8
database: db_name
username: my_user
password: pass11121212
host: 172.30.1.12
schema_search_path: my_new_schema
So I am trying to do the same using sequel
But it ignores the field of schema_search_path
How can I set the schema_search_path for postgres

how can I fix the fe_sendauth: no password supplied error?

This is my database.yml file.
I am trying to create database for a ruby app but the error always pops up
"fe_sendauth: no password supplied"
Tried to createuser - w --no-password on the CLI ended up with the same error.
Any help to fix this please ?
default: &default
adapter: postgresql
encoding: unicode
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
database: app_production
username: app
password: <%= ENV['APP_DATABASE_PASSWORD'] %>

Rails loading incorrect database config for test env?

Have a weird issue... my tests keep running against my development database. I tried binding.pry in test_helper.rb here:
# this is the top of the file
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
binding.pry
And noticed 3 things:
ENV['RAILS_ENV']/Rails.env was set correctly to "test".
Rails configuration seems to load the correct value:
Rails.application.config.database_configuration[Rails.env]
# => {"adapter"=>"postgis",
# "encoding"=>"unicode",
# "pool"=>5,
# "username"=>"user",
# "host"=>"localhost",
# "database"=>"db_test"} <= CORRECT
... but ActiveRecord loads an incorrect value:
ActiveRecord::Base.configurations[Rails.env]
# => {"adapter"=>"postgis",
# "encoding"=>"unicode",
# "pool"=>5,
# "username"=>"user",
# "host"=>"localhost",
# "port"=>5432,
# "database"=>"db_development"} <= INCORRECT
Additionally, ActiveRecord::Base.configurations[Rails.env] returns a port key whereas Rails.application.config.database_configuration[Rails.env] does not.
Why are these different? Here's my config:
default: &default
adapter: postgis
encoding: unicode
pool: 5
username: user
host: localhost
development:
<<: *default
database: db_development
test:
<<: *default
database: db_test
I greped my entire project folder and config/database.yml is the only place I name my databases, so there's not another config overriding this one as far as I know.
I'm running rails 4.2.5.
Help!
Try removing DATABASE_URL from your environment variables.

gitlab community unable to login

I followed the directions here https://github.com/gitlabhq/gitlab-recipes/blob/master/install/centos/README.md and have the gitlab community page up and running but I cannot login.
Someone suggested doing this:
bundle exec rake db:seed_fu RAILS_ENV=production
(in /home/git/gitlab)
I got a FATAL error suggesting no role for root, I then followed this:
FATAL: role "root" does not exist
and created a role, now I get this error:
[root#gitlab gitlab]# bundle exec rake db:seed_fu RAILS_ENV=production
== Seed from /home/git/gitlab/db/fixtures/production/001_admin.rb
rake aborted!
ActiveRecord::StatementInvalid: PG::Error: ERROR: permission denied for relation users
: SELECT 1 AS one FROM "users" WHERE "users"."email" = 'admin#example.com' LIMIT 1
/home/git/gitlab/vendor/bundle/ruby/2.1.0/gems/seed-fu-2.3.1/lib/seed-fu/runner.rb:46:in `eval'
[root#gitlab gitlab]# sudo -u git -H cat config/database.yml
#
# PRODUCTION
#
production:
adapter: postgresql
encoding: unicode
database: gitlabhq_production
pool: 10
# username: git
# password:
# host: localhost
# port: 5432
#
# Development specific
#
development:
adapter: postgresql
encoding: unicode
database: gitlabhq_development
pool: 5
username: postgres
password:
#
# Staging specific
#
staging:
adapter: postgresql
encoding: unicode
database: gitlabhq_staging
pool: 5
username: postgres
password:
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test: &test
adapter: postgresql
encoding: unicode
database: gitlabhq_test
pool: 5
username: postgres
password:
Please help. I do not know ruby and I also don't know postgresql

Running both MongoDB and Mysql on Rails 2.3.6

In Rails 2.3.6 I'm trying to use both MongoDb via MongoMapper and Mysql on the same application.
I've seen several others question about this but I cannot find how to configure rails to use both database in the same app.
How should I configure my database.yml file?
Right now it is:
development:
adapter: mysql
encoding: utf8
reconnect: false
database: blinddog_development
pool: 5
username: root
password:
socket: /tmp/mysql.sock
test:
adapter: mysql
encoding: utf8
reconnect: false
database: blinddog_rails_test
pool: 5
username: root
password:
socket: /tmp/mysql.sock
production:
adapter: mysql
encoding: utf8
reconnect: false
database: blinddog_rails_production
pool: 5
username: root
password:
socket: /tmp/mysql.sock
Should I add some intializers?
Thanks in advance,
Augusto
So you would use active record and standard database.yml file. You would also run rails g mongo_mapper:config to create your mongo.yml file. These are seperate and should allow you to use both in your model. This would work for mongoid too.

Resources