0

I am using rails 3.2.9 and database of postgres.

I have edited database.yml file in the following way and also gem pg was also installed..

When I run bundle command it shows pg gem in that list. But if I run the rails server command it shows the active connection not established.

    development:
      adapter: postgresql
      database: db_pchamara/development.postgresql
      pool: 5
      username: database username
      password: password
      host: database ip address
    
    test:
      adapter: postgresql
      database: database name/test.postgresql
      pool: 5
      username:database username
      password: password
    
    production:
      adapter: postgresql
      database: database name/production.postgresql
      pool: 5
      username: database username
      password: password

But when I run command rails server it shows as follows:

[root@vdimc04 my_app]# rails server
=> Booting WEBrick
=> Rails 3.2.9 application starting in development on http
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-12-06 12:49:35] INFO  WEBrick 1.3.1
[2012-12-06 12:49:35] INFO  ruby 1.9.3 (2012-11-10) [i686-linux]
[2012-12-06 12:49:35] INFO  WEBrick::HTTPServer#start: pid=7536 port=3000


Started GET "/assets/rails.png" for 127.0.0.1 at 2012-12-06 12:49:53 +0530

ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished):
  activerecord (3.2.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection'
  activerecord (3.2.9) lib/active_record/connection_adapters/abstract/connection_specification.rb:171:in `retrieve_connection'
  activerecord (3.2.9) lib/active_record/connection_adapters/abstract/connection_specification.rb:145:in `connection'
  activerecord (3.2.9) lib/active_record/query_cache.rb:67:in `rescue in call'
  activerecord (3.2.9) lib/active_record/query_cache.rb:61:in `call'
  activerecord (3.2.9) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  activesupport (3.2.9) lib/active_support/callbacks.rb:405:in `_run__626989987__call__446659449__callbacks'
  activesupport (3.2.9) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.9) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  activesupport (3.2.9) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.9) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/reloader.rb:65:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.9) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.9) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.9) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.9) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.1) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.9) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.1) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.9) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.9) lib/rails/engine.rb:479:in `call'
  railties (3.2.9) lib/rails/application.rb:223:in `call'
  rack (1.4.1) lib/rack/content_length.rb:14:in `call'
  railties (3.2.9) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.1) lib/rack/handler/webrick.rb:59:in `service'
  /usr/local/rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /usr/local/rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /usr/local/rvm/rubies/ruby-1.9.3-p327/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'


  Rendered /usr/local/rvm/gems/ruby-1.9.3-p327/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.3ms)
  Rendered /usr/local/rvm/gems/ruby-1.9.3-p327/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (3.0ms)
  Rendered /usr/local/rvm/gems/ruby-1.9.3-p327/gems/actionpack-3.2.9/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (29.4ms)
    
Please help me to solve this problem.
Dynamo
  • 67
  • 11
Pavan Kumar
  • 1,735
  • 2
  • 28
  • 47
  • 1
    try to name your dbs like this : `my_app_name_development`, and be sure to create them via `db:create` – m_x Dec 06 '12 at 07:48
  • i am running a dummy project that was just created in terminal.. To test wether the connection was established or not, simply i edited the database.yml file and installed the pg gem... Do i need to do any more to know my db connection has done or not??? – Pavan Kumar Dec 06 '12 at 11:35
  • 1
    Stupid questions, but... Is postgreSQL installed ? Did you create your user using `psql` ? did you configure your `pg_hba.conf` and `postgresql.conf` to accept connexions on localhost with password or md5 authentication ? Is the postgres server running ? did you run `rake db:create` ? To ensure you're good, just run `rails console` and try `ActiveRecord::Base.connection` – m_x Dec 06 '12 at 11:40
  • @m_x postgresql has installed and it is running also.. if i run postgres through ruby program sit brings the data..but while coming through rails it wont works – Pavan Kumar Dec 06 '12 at 11:55
  • what are the names of your dbs ? do you still have issues starting the server / console ? – m_x Dec 06 '12 at 13:02
  • yes...names are production: database: db_pchamara test: database: db_pchamara.test production: database: database: db_pchamara I am still ahving the same error.. if i creates a empty project it connects to sqlite database ad everything is fine then.. but if i make changes to database.yml to postgres , it is not running – Pavan Kumar Dec 06 '12 at 13:20
  • my guess is that your database.yml is not parsed correctly. try to remove any dot (`.`) in your db names. If it still does not work, try to parse it with YAML or Psych – m_x Dec 06 '12 at 14:39
  • how to parse my dml file with YAML or psych.. I dont know how to do ???? Can u explain me briefly?? – Pavan Kumar Dec 08 '12 at 06:22
  • http://stackoverflow.com/questions/3877004/how-do-i-parse-a-yaml-file – m_x Dec 08 '12 at 11:30

1 Answers1

1

The problem is in the configuration of the file:

development:
  adapter: postgresql
  database: db_pchamara/development.postgresql
  pool: 5
  username: database username
  password: password
  host: database ip address

change it to:

development:
  adapter: postgresql
  database: name_of_database
  pool: 5
  username: db_user
  password: db_pass
  host: db_host

most of the time db_host is just localhost

also, make sure you can connect with psql before you actually try to connect with rails.

if the db doesn't exist, you have to create the db with

rake db:create

cheers ;)

rorra
  • 9,593
  • 3
  • 39
  • 61
  • can the development ,test and production in database.yml everything can be same?????? – Pavan Kumar Dec 06 '12 at 11:00
  • sir i am running a dummy project that was created by aptana ide.. To test wether the connection was established or not, simply i edited the database.yml file and installed the pg gem... Do i need to do any more to know my db connection has done or not???? – Pavan Kumar Dec 06 '12 at 11:27
  • @rorra this is a dangerous advice. at least the db name should be different ! – m_x Dec 06 '12 at 11:37
  • Is not an advice, is an answer to his question, if he can have the same configuration for dev/test/prod, which in a local environment for developing is not bad at all. – rorra Dec 06 '12 at 12:29
  • and you use the _same_ db for testing and dev ? yuck. – m_x Dec 06 '12 at 13:00
  • or I don't even write testing code unless the client has the money to pay for the testing code time :) – rorra Dec 06 '12 at 13:53
  • @rorra sir the problem hasnt solved till now... the server shows the same error and if run the ruby file with db connection, it gets the data.. but in rails server it wont makes a connection – Pavan Kumar Dec 06 '12 at 13:59
  • i am receiving the error like password not suplied while i run rake db:migrate.. but in my database.yml file i have given my password... help me plz.... – Pavan Kumar Dec 08 '12 at 06:20
  • That's really weird, rake db:migrate is the same that rake db:create or rake db:drop. If the configuration is written on config/database.yml and you have the right parameters (user, password, etc.) I don't see a reason of why that happens. What's the value of the environment variable RAILS_ENV ? run "puts $RAILS_ENV" and let me know. Other way I can help if that if you open a remote connection, like with teamviewer or something I'll be able to help. I'm really sorry that you had stuck with this issue so long, its usually a 5 minute configuration. – rorra Dec 08 '12 at 06:31
  • @rorra i solved this issue.. This is because of pg_hba.conf file in posstgres installation. we have to change some modifications to that file.. Thank u for ur support.. – Pavan Kumar Dec 15 '12 at 05:14
  • @rorra but now i am having an another issue that when send parameter through URL , itshows ACTIVE RECORD NOT FOUND..couldnt find product without ID.. i am sending ID as localhost:3000/getproducts/PR1.json where PR1 is my product id in db.. help me in this sir – Pavan Kumar Dec 15 '12 at 05:16
  • Could you open a new question a put more information about how the table looks like, and the code you have that is failing? Also, add these two gems to your Gemfile, it will really help you to fix errors whenever you see them, and it will give more information about them: gem "better_errors"; gem "binding_of_caller" – rorra Dec 15 '12 at 05:23
  • @rorra can you please take a look at a question i asked i am suck in a similar situation. i would appreciate it if you can please help me. http://stackoverflow.com/questions/29970739/rails-app-on-heroku-deployed-successfully-but-does-not-run-query – user1010101 Apr 30 '15 at 17:53