I have spend several hours looking for solutions to this on the web. Postgresql installation guides do not help since i am not familiar with interacting with databases only with ruby on rails and dealing with them this way.
I have made several simple rails application by following tutorials and am still learning rails. I am using Windows 10 and I have deployed some of these to Heroku for which i had to use the pg gem becasue Heroku doesnt use sqlite3. But I have read in several places that it is necessary to install postgresql locally. So i have installed it, i think, but now i am lost. I have a software called pgAdmin4 and now i have absolutely no idea what to do in order to use postgresql through rails. I started a new app just to test it by using the following command:
rails new postgresapp --database=postgresql
This has given me the basic starting point for a new app set up to use postgresql. the first error that I get when i try to connecct to the localhost3000 is:
PG::ConnectionBad
fe_sendauth: no password supplied
Extracted source (around line #56):
### Convenience alias for PG::Connection.new.
def self::connect( *args )
return PG::Connection.new( *args )
end
bearing in mind that i am not sure if the installation was a success. i went through the process of installation and entered a password and all the information so i assume that it did install somewhere.
also there are no servers showing on pgAdmin so i added one called localhost. i am not sure if this was the right thing to do or not.
gem file:
source 'https://rubygems.org'
git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
"https://github.com/#{repo_name}.git"
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.6'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
database.yml
# PostgreSQL. Versions 9.1 and up are supported.
#
# Install the pg driver:
# gem install pg
# On OS X with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
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 } %>
development:
<<: *default
database: postgresapp_development
# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user that initialized the database.
#username: postgresapp
# The password associated with the postgres role (username).
#password:
# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost
# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice
# 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:
<<: *default
database: postgresapp_test
# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="postgres://myuser:mypass#localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:
<<: *default
database: postgresapp_production
username: postgresapp
password: post#<%= ENV['POSTGRESAPP_DATABASE_PASSWORD'] %>
When you check what is in your database.yml file you need user and password configuration.
You have password as blank value.
I would suggest create a postgreSql user with password and use those credentials
You will need psql for accessing postgreSql from terminal, to create new user with permissions.
one of the links, you can find a lot more about this on the web.
But you problem is user and his password
After fixing you user and password problems, you will need to create the DB with the name from database.yml.
In you database.yml DB name is postgresapp_development. You will ne to create it. Find a client for windows to connect to you postgreSql with user and password you defined when installing and create the DB.
Check this for accessing you postgresSql server
Related
I am using the following gems in my application.
gem 'activerecord-session_store'
gem 'sidekiq'
gem 'sidekiq-unique-jobs'
gem 'sidekiq-cron'
gem 'sinatra', require: false
gem 'capistrano-sidekiq', github: 'seuros/capistrano-sidekiq'
I recently started using activerecord-session_store and in the process I remember having to update my secret_key_base for my application. Now when I try to make changes through the SideKiq admin panel I always get a blank white page that says "forbidden". I'm guessing that my session is not being shared correctly? How can I set up SideKiq to use the same secret_key_base as my application?
You can do the following to share Rails' secret key base with Sidekiq web interface:
Sidekiq::Web.set :session_secret, Rails.application.secrets[:secret_key_base]
I am currently following 'Ruby on Rails Tutorial' by Michael Hartl. On Chapter 3 when I try to deploy the working sample app with Heroku it is rejected, and even though I fully understand the error message I do not know how to correct it. Here is my Gemfile:
source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.5'
group :development, :test do
gem 'sqlite3', '1.3.8'
gem 'rspec-rails', '2.13.1'
end
group :test do
gem 'selenium-webdriver', '2.35.1'
gem 'capybara', '2.1.0'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
group :production do
gem 'pg', '0.15.1'
gem 'rails_12factor', '0.0.2'
end
And here is the file where the issue originates which is config/environments/development.rb:
SampleApp::Application.configure do
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
config.cache_classes = false
# Do not eager load code on boot.
config.eager_load = false
# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
# Print deprecation notices to the Rails logger.
config.active_support.deprecation = :log
# Raise an error on page load if there are pending migrations.
config.active_record.migration_error = :page_load
# Debug mode disables concatenation and preprocessing of assets.
# This option may cause significant delays in view rendering with a large
# number of complex assets.
config.assets.debug = true
# Adds additional error checking when serving assets at runtime.
# Checks for improperly declared sprockets dependencies.
# Raises helpful error messages.
config.assets.raise_runtime_errors = true
# Raises error for missing translations
# config.action_view.raise_on_missing_translations = true
end
The errors I get are attempting 'git push heroku master':
Running: rake assets:precompile
rake aborted!
NoMethodError: undefined method `configure' for #<SampleApp::Application:0x007fc7f03f7298>
and after a long list of paths I get:
!
! Precompiling assets failed.
!
! Push rejected, failed to compile Ruby app
To git#heroku.com:some-name.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git#heroku.com:some-name.git'
Any idea how to fix this please? I really would like to get deployments working under Ubuntu.
Thank you!
By default Rails 4 will not serve your assets. To enable this functionality you need to go into config/application.rb and add this line:
config.serve_static_assets = true
Have a read through this documentation on heroku as well - https://devcenter.heroku.com/articles/rails-4-asset-pipeline
Hope it helps. :)
Rails 4
The problem is a common issue with the Hartl tutorial - it derives from using Rails 3 in the tutorial, and a later version being used widely now
I tried looking for references on this, but I couldn't. I've found them somewhere before - will have another look for you.
Anyway, in Rails 4, need this (we have this in our Rails 4 apps):
#config/environments/development.rb
Rails.application.configure do
This sets the configure code block for the correct object. I think Rails changed some of its core functionality in 4.0+, which is why you're getting the issue now
I am starting rails by following this tutorial:
http://ruby.railstutorial.org/ruby-on-rails-tutorial-book#sec-the_first_application
which seems very nice.
At the beginning, the author talks about the importance of the versions for the gems and softwares, so I did my best to keep using the exact same versions.
I followed the tutorial and it all ran nicely, the installation was ok (from his suggested source: http://railsinstaller.org/en )I downloaded ruby 1.9.
After installing, I used rails new first_app to create my app, changed the Gemfile to this one:
source 'https://rubygems.org'
ruby '1.9.3' #In the tutorial is 2.0.0, but changed to match my ruby version,
#as specified in the tutorial
#ruby-gemset=railstutorial_rails_4_0
gem 'rails', '4.0.1'
group :development do
gem 'sqlite3', '1.3.8'
end
gem 'sass-rails', '4.0.1'
gem 'uglifier', '2.1.1'
gem 'coffee-rails', '4.0.1'
gem 'jquery-rails', '3.0.4'
gem 'turbolinks', '1.1.1'
gem 'jbuilder', '1.0.2'
group :doc do
gem 'sdoc', '0.3.20', require: false
end
When I run rails server command, I get the following error:
DEPRECATION WARNING: config.whiny_nils option is deprecated and no longer works.
(called from block in <top (required)> at D:/rails/first_app/config/environment
s/development.rb:10)
config.eager_load is set to nil. Please update your config/environments/*.rb fil
es accordingly:
* development - set it to false
* test - set it to false (unless you use a tool that preloads your test enviro
nment)
* production - set it to true
But opening localhost:3000 works fine. Clicking on "About your application’s environment" link, generates an error
ActiveRecord::ConnectionNotEstablished
Rails.root: D:/rails/first_app
I checked and my database.yml is using sqlite3.
When I run rake db:create inside my app's folder, I get
rake aborted!
Specified 'postgresql' for database adapter, but the gem is not
loaded. Add gem 'pg' to your Gemfile.
I think that maybe these three issues are connected and the source of the problem is that error message when starting the rails server.
What can I do to fix it, could it be the ruby version 1.9.3 instead of 2.0.0?
thanks!
EDIT:
On this link, I found the solution for my issue on the whiny_nils deprecation
Rails 4 removed the whiny_nils feature. Read more about it in the ActiveRecord chapter.
To solve the deprecation warning, simply remove any lines that set config.whiny_nils. Rails 3 added the configuration by default in config/environments/development.rb and config/environments/test.rb by default.
No idea why creating an app and starting it with the same version would cause this problem, but ok. No.1 fixed :)
Edit2: In the same link, I fixed the config.eager_load issue by creating this config in my config files and setting a value.
The active record issue remains.
EDIT 3:
This is my database.yml file
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
# 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:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
Solved: I had a database_url pointing to a postgres DB in my system environment variables. I've done that when following an heroku tutorial long ago. I removed it and now it works fine.
I suggest a solution that I believe will work better for you:
Remove the version numbers from all of the gems other than rails
It will do rails 4 and ruby 2.0 and for you ruby 2.0 will probably work.
If necessary have the ruby version as 1.9.3
This would probably work better for you now and in the future.
It's better to avoid all those specific version numbers for other gems to avoid having.... the version problems you are experiencing. You want to spend less time on that and more time on the actual app, rails code, ruby code, etc. Most gems can figure out the right versions and dependencies themselves.
Try quickly doing another app this way (make sure you "cd .." out of this app first before issuing that rails new command again). You'll also see that doing a new app is a surprisingly frequent thing when compared to some other, older frameworks.
This question already has answers here:
Rails server does not start by command "Rails s"
(6 answers)
Closed 9 years ago.
$ rails server
Usage:
rails new APP_PATH [options]
Options:
-r, [--ruby=PATH] # Path to the Ruby binary of your choice
# Default: /home/bikram/.rvm/rubies/ruby-1.9.3-p448/bin/ruby
-b, [--builder=BUILDER] # Path to a application builder (can be a filesystem path or URL)
-m, [--template=TEMPLATE] # Path to an application template (can be a filesystem path or URL)
[--skip-gemfile] # Don't create a Gemfile
[--skip-bundle] # Don't run bundle install
-G, [--skip-git] # Skip Git ignores and keeps
-O, [--skip-active-record] # Skip Active Record files
-S, [--skip-sprockets] # Skip Sprockets files
-d, [--database=DATABASE] # Preconfigure for selected database (options:
mysql/oracle/postgresql/sqlite3/frontbase/ibm_db/sqlserver/jdbcmysql/jdbcsqlite3/jdbcpostgresql/jdbc)
# Default: sqlite3
-j, [--javascript=JAVASCRIPT] # Preconfigure for selected JavaScript library
# Default: jquery
-J, [--skip-javascript] # Skip JavaScript files
[--dev] # Setup the application with Gemfile pointing to your Rails checkout
[--edge] # Setup the application with Gemfile pointing to Rails repository
-T, [--skip-test-unit] # Skip Test::Unit files
[--old-style-hash] # Force using old style hash (:foo => 'bar') on Ruby >= 1.9
Runtime options:
-f, [--force] # Overwrite files that already exist
-p, [--pretend] # Run but do not make any changes
-q, [--quiet] # Suppress status output
-s, [--skip] # Skip files that already exist
Rails options:
-h, [--help] # Show this help message and quit
-v, [--version] # Show Rails version number and quit
Description:
The 'rails new' command creates a new Rails application with a default
directory structure and configuration at the path you specify.
You can specify extra command-line arguments to be used every time
'rails new' runs in the .railsrc configuration file in your home directory.
Note that the arguments specified in the .railsrc file don't affect the
defaults values shown above in this help message.
Example:
rails new ~/Code/Ruby/weblog
This generates a skeletal Rails installation in ~/Code/Ruby/weblog.
See the README in the newly created application to get going.
It generated like this but not rails local server. Other versions are:
Rails 3.2.14
ruby 1.9.3p448 (2013-06-27 revision 41675) [i686-linux]
gem 2.1.9
rvm 1.23.9 (stable)
my gem file is:
source 'https://rubygems.org'
gem 'rails', '3.2.14'
group :development, :test do
gem 'sqlite3', '1.3.5'
gem 'rspec-rails', '2.11.0'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '3.2.5'
gem 'coffee-rails', '3.2.2'
gem 'uglifier', '1.2.3'
end
gem 'jquery-rails', '2.0.2'
group :test do
gem 'capybara', '1.1.2'
end
group :production do
gem 'pg', '0.12.2'
end
This is the default message generated when you try to launch the Rails server in a folder without a Rails application.
You need to run rails new <appname> where appname is the folder you're in prior to launching the server.
If it is a Rails application, then you're likely missing some key files that the server command uses to determine if it is a Rails application.
I guess you are running the command rails server while not in (the root directory) of a Rails application.
Change into your Rails project directory and try again.
The following is the error message:
/Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:47:in `resolve_hash_connection'
/Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb:41:in `resolve_string_connection'
...
/Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/application.rb:103:in `require_environment!'
/Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/railties-3.2.13/lib/rails/application.rb:297:in `block (2 levels) in initialize_tasks'
/Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/bin/ruby_noexec_wrapper:14:in `eval'
/Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/bin/ruby_noexec_wrapper:14:in `<main>'
My database.yml file:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
My Gemfile:
source 'https://rubygems.org'
gem "therubyracer"
gem "less-rails" #Sprockets (what Rails 3.1 uses for its asset pipeline) supports LESS
gem "twitter-bootstrap-rails"
gem 'jquery-rails'
gem 'devise'
gem 'rails', '3.2.13'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'
group :development, :test do
gem 'sqlite3'
end
group :production do
gem 'pg'
end
gem 'mini_magick'
gem "rmagick"
gem "carrierwave"
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', :platforms => :ruby
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
# gem 'unicorn'
# Deploy with Capistrano
# gem 'capistrano'
# To use debugger
# gem 'debugger'
I solved a similar problem in a somewhat intricate project. Not sure it's directly related, but I'm posting this as the way of debugging the problem may be helpful.
In my case, I had the following scenario:
The situation only occurred when RAILS_ENV=production. When I did RAILS_ENV=development it worked. Weirdly enough, when I changed the production entry in database.yml to production2 and ran the command with RAILS_ENV=production2, it worked.
In the project I was connecting to multiple database connections through various models and libraries.
Here is what I did to detect the issue:
vim /Users/davidzabner/.rvm/gems/ruby-1.9.3-p448/gems/activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb
(or wherever the backtrace is telling you the issue is).
Then, I found the place in the code that has these lines:
def resolve_hash_connection(spec) # :nodoc:
spec = spec.symbolize_keys
raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)
And changed it to the following:
def resolve_hash_connection(spec) # :nodoc:
spec = spec.symbolize_keys
# Debug printing
puts "*" * 80, spec.inspect, "*" * 80
raise(AdapterNotSpecified, "database configuration does not specify adapter") unless spec.key?(:adapter)
Then I reran the command, in my case bundle exec rails c production.
By doing this I realized that Rails wasn't looking for the production entry as I was thinking it was. It was looking for a different entry called abc_production, which was required in my project due to the multiple database connections I was referring to earlier. On that particular server someone forgot to add that abc_production entry to database.yml. Adding the entry solved the issue.
I believe it happened only when RAILS_ENV=production because in environments/production.rb I have config.eager_load = true, which means Rails will eager-load the application and classes to memory, and probably attempt to establish all database connections defined in those classes (one of them being abc_production).
Hope this helps someone in a similar situation... If you're not using multiple connections, try and debug the problem by changing connection_specification.rb and see if it gives you any lead..
i don't now exactly what you tried to do but.
currently i got the same error as i tried to run < rails c -e production > with ruby-2.10.
`resolve_hash_connection': database configuration does not specify adapter (ActiveRecord::AdapterNotSpecified)
as i run < rails c production > everything works.
maybe this helps someone
I had a similar issue with staging environment and I did:
added devise secret to devise.rb initializer file
configured staging 'secret_key_base' in secrets.yml
It worked fine me there after.
I've crated an gist with a sample database configuration, please make sure you use it.
The gist is located at: https://gist.github.com/fidalgo/5970617
Also make sure you run rake db:setup to setup your database.
Also since in your environment you are using Sqlite for production and test too, in your Gemfile change this lines:
group :development, :test do
gem 'sqlite3'
end
to
#group :development, :test do
gem 'sqlite3'
#end
it shouldn't make any significant difference, except you are using other environment than development.
I met this problem too. I tried all similar questions I can find but none of them resolved my problem then I tried to print configurations system read from database.yml in activerecord-3.2.13/lib/active_record/connection_adapters/abstract/connection_specification.rb
def resolve_string_connection(spec) # :nodoc:
hash = configurations.fetch(spec) do |k|
connection_url_to_hash(k)
end
p configurations
p spec
raise(AdapterNotSpecified, "#{spec} database is not configured") unless hash
resolve_hash_connection hash
end
The output is
{"production"=>nil, " adapter"=>"mysql2", " encoding"=>"utf8mb4", " username"=>"myUsername", " password"=>"myPassword", " pool"=>5, " database"=>"mydb", " host"=>"myHost", " port"=>3306, "mydb_production"=>nil}
So my problem is there's something wrong in the database.yml causes YAML parse error. However I don't find anything wrong by my bare eyes so I copied a file from another server then problem resolved.
Hope this can help someone :p
Realise this is a little old, but have just come across whilst looking for an answer on a similar issue. Just wanted to point out that you still have sqlite3 as your adaptor in your .yml. As you have probably found out since, Heroku does not allow sqlite3 as a production db. SQLite on Heroku