Requesting a ruby-on-rails application gives Psych::BadAlias error - ruby-on-rails

I ran the rails server from my application directory.
But when I tried to do a request from the browser with
http://localhost:3000
I get the following error:
*Psych::BadAlias
Cannot load Rails.application.database_configuration: Unknown alias: default*

This happens because the ruby-plugin for netbeans messes the database.yml file when creating project from existing source. It replaces the comment about sqlite3 to mysql, but even worse it erases the first definition block which is 'default'
So simply insert
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
at the beginning of the database.yml and you'll be fine

This is being caused by an invalid alias in your database.yml file. You most likely have something that looks close to this... but not quite:
defaults: &defaults
adapter: mysql2
username: root
password:
host: localhost
timeout: 5000
development:
database: app_name_development
<<: *defaults
In this, &defaults defines the alias that your error is talking about. Make your database.yml look more like this syntax and you should be set. Or, post your database.yml file here and we can help you fix it specifically if needed.

The content of the database.yml file is:
development:
<<: *default
database: db/development.sqlite3
test:
<<: *default
database: db/development.sqlite3_test
production:
<<: *default
database: db/development.sqlite3_production
I am using netbeans as text editor and there is a error at the top of the file indicating:
ComposerException null we had this found undefined alias default
Thank you

You might need to upgrade your ruby > 1.9.

This happens because the password DB has some special character if you used rbenv-vars Plugin, change the password without special character.

The pysch gem has a breaking change where it will not allow loading YAML files containing aliases by default.
Here's an example of a YAML:
default: &default
adapter: postgresql
Since you don't control the code in question you can either modify your yml files (e.g. database.yml) to not use aliases or monkey patch the psych code.
There is a lot more detail in this answer, even though the error message has been updated.
visit_Psych_Nodes_Alias: Unknown alias: default (Psych::BadAlias)

Related

ActiveRecord connection requires reset?

An application in production mode is returning content as expected. It is assumed it is connecting to the configured database. The database.yml file is as follows
default: &default
adapter: postgresql
user: deploy
password: 888xxx888xxx
schema_search_path: public
# For details on connection pooling, see rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: 5
timeout: 10000
development:
<<: *default
database: fort_development
<<: *default
database: fort_test
production:
<<: *default
database: fort_production
However, when connecting to the database via the console, whether in development (where the db does not exist) or in production, the console is connecting to the wrong database. It is also referencing a username which was removed from the database.yml file (and app subsequently restarted).
note: at one time the string myapp was referenced in the yml file for the schema_search_path attribute, however it was a commented out line (There is no certainty as to whether it was ever activated.'
> bundle exec rails c
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
> bundle exec rails c production
irb(main):001:0> ActiveRecord::Base.connection_config
=> {:adapter=>"postgresql", :user=>"deploy", :password=>"888xxx888xxx", :schema_search_path=>"public", :pool=>5, :timeout=>10000, :database=>"myapp", :username=>"deploy", :host=>"127.0.0.1"}
The console and the app are not in synch. What can make the console work with different values and how can it be reset to work with the proper configuration data?
For anyone stumbling upon this, there is another place where rake can take instructions for connecting via ActiveRecord.
.rbenv-vars
this file within the directory for the application was mistakenly set as:
DATABASE_URL=postgresql://deploy:PASSWORD#127.0.0.1/myapp
Another potential sources of problems (not clear why as the files properly parses in yaml) is to have the database.yml file only reference the production environment
production:
adapter: postgresql
database: fort_production
password: [...]
pool: 5
schema_search_path: public
timeout: 10000
user: deploy

Rails/PostgreSQL problem: PG::ConnectionBad: fe_sendauth: no password supplied

I'm trying to run my tests for my app but I keep getting both this errors:
PG::ConnectionBad: fe_sendauth: no password supplied
PG::ConnectionBad: FATAL: sorry, too many clients already
I'be read all previous answers and it seems to be, for everybody else, either a missing password or a misconfiguration of the pg_hba.conf file.
This is how my database.yml looks like:
default: &default
adapter: postgresql
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000
encoding: utf8
reconnect: true
host: localhost
username: postgres
password: <password>
development:
<<: *default
database: <dev-db>
host: <dev-host>
username: <dev-user>
password: <dev-pass>
test:
<<: *default
database: <test-db>
I already checked the pg_hba.conf config file and its configured to md5 for all entries listed.
Any clues to what might be the problem?
I'm running PostgreSQL 10, Rails 5.2.3 on Ruby 2.5.5, on a Macbook with Mojave.
Thanks in advance.
Does your postgres user actually have a password set on it? Typically there is no password for the default postgres user, so this is something you would need to have gone out of your way to do; judging by the error message, it sounds like you are using the default posgres user and have not gone out of your way to set a password in the PostgreSQL CLI.
I suggest stripping it down to a very basic default, functional config like:
postgres: &postgres
adapter: postgresql
encoding: utf8
pool: 5
development:
<<: *postgres
database: project_name_development
test:
<<: *postgres
database: project_name_test
Then incrementally add back additional configuration options one at a time, confirming that each one works before adding the next. Env vars also may not be available to database.yml depending on your project configuration and installed gems.

Setting the connection pool (and other database options) using Heroku

From this page, it looks like Heroku does most of the work in building your database.yml file on deployment, meaning that all I have to have is my development and test environment configurations.
Is there a way I can also specify production values? For example, if I wanted to set pool: 25 on production, can I include that in my database.yml as follows?
development: &default
adapter: postgresql
database: my_app_development
encoding: utf8
host: localhost
min_messages: warning
pool: 2
timeout: 5000
test:
<<: *default
database: my_app_test
-- Can I add this, or will it get overwritten?
production:
<<: *default
pool: 25
Actually, found the answer to exactly my question on the Heroku documentation
https://devcenter.heroku.com/changelog-items/426
Looks like whatever options you set will be merged in and preserved, so I can indeed set connection pool in my database.yml

Heroku Push Errors

Good afternoon,
I browsed the other questions on this topic, and it appears nobody has asked about this issue. I was expecting otherwise!
In any case - I have a PostgreSQL database server running on my local machine with a MYAPP_DEVELOPMENT database. I've tried to do a
heroku db:push
But keep getting the following error:
Failed to connect to database:
Sequel::DatabaseConnectionError -> PGError: FATAL: role "brandon" does not exist
This obviously has something to do with the permissions & users on the local/heroku shared database, but I'm honestly not that good with this kind of stuff. Any help would be appreciated. I'm currently including the "database.yml" file in my slug to Heroku, which has all of the login/password info for my local database... hence why I was not expecting this kind of error.
Thanks!
** EDIT **
Here is the contents of my database.yml file (edited for clarity):
common: &common
adapter: postgresql
encoding: unicode
username: user
password: secret
test:
<<: *common
database: myapp_test
development:
<<: *common
database: myapp_development
production:
<<: *common
database: myapp_production
I figure it must be something on the Heroku setup side. Note that nowhere in my database.yaml file does "brandon" show up. I'm not quite sure where it is pulling that from. My database user name is not that (although that is my name haha)
This error generally means a username has not been specified under the appropriate environment in your database.yml file.
Your database.yml file should resemble the following (I use MySQL locally, hence the adapter and socket parameters):
common: &common
adapter: mysql2
encoding: utf8
reconnect: false
pool: 5
username: <your_username>
password: <your_password>
socket: /tmp/mysql.sock
development:
<<: *common
database: appname_dev
test:
...
staging:
...
production:
<<: *common
database: appname_prod
If that doesn't solve your problem, check to ensure Heroku is running in the correct environment:
heroku config --app <appname>
Ensure RACK_ENV is set to production
If not: heroku config:add RACK_ENV=production --app <appname>
(It would have been helpful if you pasted the contents of your database.yml file.)
You will have to use the Heroku specific ENV var to use the DB which is usually ENV['DATABASE_URL']
Edit: If you have a db mapper like datamapper you'd write it like this:
DataMapper.setup(:default, ENV['DATABASE_URL'] || LOCAL_DB_URL)
which then either uses the Heroku DB if it exists or defaults to your local DB which is stored in the LOCAL_DB_URL var.
Thanks for everyone's input to this one. I ended up fixing my issue by explicitly providing my login details to Heroku during the db:push... e.g.:
heroku db:push postgres://postgres:PASSWORD#127.0.0.1:5432/DATABASE
Not sure why it isn't using the (same) information in my database.yml file, but oh well.

Rake db error: ERROR 1102 (42000) at line 1: Incorrect database name [nil]

I am getting the following error:
$ rake db:drop
(in D:/Repository/MyApp)
~ [datamapper] Setting up the "development" environment:
~ [datamapper] Setting up :default repository: '' on mysql
ERROR 1102 (42000) at line 1: Incorrect database name ''
The following is my database.yml file:
defaults: &defaults
adapter: mysql
encoding: utf8
reconnect: false
pool: 5
username: dbuser
password: ******
development:
database: myapp_development
host: 127.0.0.1
<<: *defaults
test:
database: myapp_test
host: 127.0.0.1:3306
<<: *defaults
production:
database: myapp_production
host: mysql.myapp.com
<<: *defaults
I'm running a mysql 5.5 server on a Windows platform, with Ruby 1.9.2 and Rails 3, and the server is configured with the proper databases, user and password. I'm using datamapper. This works on our live site, but not on my local site. Running 'rails dbconsole' produces "ruby192installationpath/dbconsole.rb:75: in 'exec': can't convert nil into String (TypeError).
Has anyone run into this problem before? Any suggestions on how to diagnose or correct it? Or perhaps a simple diagnostic command that can be run from the terminal, rails or rake consoles to expose some information? Perhaps one that can verify it's connecting to the database, or if not what the problem is?
I was experiencing that same error and found through another post that it was actually caused by a YAML parsing error.
That made remember that when setting up Mongoid with Ruby 1.9.2, I had to include the following in config/environment.rb
#Add this to config/environment.rb at the top
require 'yaml'
YAML::ENGINE.yamler= 'syck'
That did the trick and solved this issue.
Hope it helps.

Resources