Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I switched my development database from SQLite to postgresql. My app worked in both development and production (Heroku, postgresql) but now I cannot modify, delete or create records in the new postgresql development database. The user can signin, see their profile information and signout correctly only.

Error on create user:

ActiveRecord::StatementInvalid in Devise::RegistrationsController#create

Errno::EBADF: Bad file descriptor - rb_thread_select(): INSERT INTO "users" ("field1", "field2", "field3", ...etc continues through..., "Field28") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28) RETURNING "id"

Error on delete user:

ActiveRecord::StatementInvalid in Devise::RegistrationsController#destroy

Errno::EBADF: Bad file descriptor - rb_thread_select(): DELETE FROM "users" WHERE "users"."id" = $1

This all behaves correctly in production.

I am using Cygwin on Windows 7 and used the pgAdmin Windows package to install postgresql.

I created the new database 4 different times using the following methods all of which produced the same results above.

  1. Created an empty database in pgAdmin and using rake db:migrate created the fields correctly but the app is unable to create records.

  2. Created an empty database in pgAdmin then used db:schema:load with the same results as #1.

  3. Created an empty database in pgAdmin. Used pgbackups to create a .dump file from my production app then used pgrestore into the new empty database. All looks good, fields and records but still unable to modify, delete or create. Can only sign in, and see the user profile.

  4. Followed Railscast #342 Migrating to PostgreSQL using all the steps, including the Taps Gem, exactly as described beginning at 4:40. Again all looked good, fields and records but same behavior as #3.

I am able to interact directly with the database either through the pgAdmin GUI or the psql mydatabase command line.

database.yml

development:
  adapter: postgresql
  encoding: unicode
  database: devdatabase
  pool: 5
  username: postgres
  password: 'password'

test:
  adapter: postgresql
  encoding: unicode
  database: testdatabase
  pool: 5
  username: postgress
  password: 'password'

My postgresql log shows successful connections to the database:

2013-04-18 15:12:09 EDT LOG:  database system was shut down at 2013-04-18 15:03:00 EDT
2013-04-18 15:12:09 EDT LOG:  database system is ready to accept connections
2013-04-18 15:12:09 EDT LOG:  autovacuum launcher started
2013-04-18 15:12:10 EDT LOG:  connection received: host=::1 port=58635
2013-04-18 15:13:14 EDT LOG:  connection received: host=::1 port=58637
2013-04-18 15:13:14 EDT LOG:  connection authorized: user=postgres database=brdevelopment
2013-04-18 15:13:25 EDT LOG:  connection received: host=::1 port=58653
2013-04-18 15:13:25 EDT LOG:  connection authorized: user=postgres database=brdevelopment
2013-04-18 15:18:32 EDT LOG:  received fast shutdown request
2013-04-18 15:18:32 EDT LOG:  aborting any active transactions
2013-04-18 15:18:32 EDT LOG:  autovacuum launcher shutting down
2013-04-18 15:18:32 EDT LOG:  shutting down
2013-04-18 15:18:32 EDT LOG:  database system is shut down

My application log shows the successful user signins, signouts and requests to see profiles. It also shows how a user can successfully change their password by clicking on a link in a confirmation email. It also shows the errors when a user tries to change their profile, delete their account or when a new user tries to signup. The errors:

  Rendered /home/Pavilion/.rvm/gems/ruby-1.9.3-p374/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
  Rendered /home/Pavilion/.rvm/gems/ruby-1.9.3-p374/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (2.0ms)
  Rendered /home/Pavilion/.rvm/gems/ruby-1.9.3-p374/gems/actionpack-3.2.11/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (42.0ms)
Connecting to database specified by database.yml
Connecting to database specified by database.yml

...

Started DELETE "/users" for 127.0.0.1 at 2013-04-18 15:14:20 -0400
Processing by Devise::RegistrationsController#destroy as HTML
  Parameters: {"authenticity_token"=>"xxxxx="}
  [1m[35mUser Load (0.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 57 LIMIT 1
  [1m[36m (0.0ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (1.0ms)[0m  DELETE FROM "users" WHERE "users"."id" = $1  [["id", 57]]
Errno::EBADF: Bad file descriptor - rb_thread_select(): DELETE FROM "users" WHERE "users"."id" = $1
  [1m[36m (0.0ms)[0m  [1mROLLBACK[0m
Completed 500 Internal Server Error in 5ms

ActiveRecord::StatementInvalid (Errno::EBADF: Bad file descriptor - rb_thread_select(): DELETE FROM "users" WHERE "users"."id" = $1):
  activerecord (3.2.11) lib/active_record/connection_adapters/postgresql_adapter.rb:1165:in `block'
  activerecord (3.2.11) lib/active_record/connection_adapters/postgresql_adapter.rb:1165:in `exec_cache'
  activerecord (3.2.11) lib/active_record/connection_adapters/postgresql_adapter.rb:674:in `block in exec_delete'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
  activesupport (3.2.11) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
  activerecord (3.2.11) lib/active_record/connection_adapters/postgresql_adapter.rb:672:in `exec_delete'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/database_statements.rb:101:in `delete'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `delete'
  activerecord (3.2.11) lib/active_record/relation.rb:413:in `delete_all'
  activerecord (3.2.11) lib/active_record/persistence.rb:141:in `destroy'
  activerecord (3.2.11) lib/active_record/locking/optimistic.rb:103:in `destroy'
  activerecord (3.2.11) lib/active_record/callbacks.rb:254:in `block in destroy'
  activesupport (3.2.11) lib/active_support/callbacks.rb:403:in `_run__747758159__destroy__128269692__callbacks'
  activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_destroy_callbacks'
  activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
  activerecord (3.2.11) lib/active_record/callbacks.rb:254:in `destroy'
  activerecord (3.2.11) lib/active_record/transactions.rb:254:in `block in destroy'
  activerecord (3.2.11) lib/active_record/transactions.rb:313:in `block in with_transaction_returning_status'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
  activerecord (3.2.11) lib/active_record/transactions.rb:208:in `transaction'
  activerecord (3.2.11) lib/active_record/transactions.rb:311:in `with_transaction_returning_status'
  activerecord (3.2.11) lib/active_record/transactions.rb:254:in `destroy'
  devise (2.2.3) app/controllers/devise/registrations_controller.rb:59:in `destroy'
  actionpack (3.2.11) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (3.2.11) lib/abstract_controller/base.rb:167:in `process_action'
  actionpack (3.2.11) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (3.2.11) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  activesupport (3.2.11) lib/active_support/callbacks.rb:436:in `_run__389569436__process_action__28646103__callbacks'
  activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
  activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.11) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (3.2.11) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
  activesupport (3.2.11) lib/active_support/notifications.rb:123:in `block in instrument'
  activesupport (3.2.11) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (3.2.11) lib/active_support/notifications.rb:123:in `instrument'
  actionpack (3.2.11) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
  actionpack (3.2.11) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
  activerecord (3.2.11) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (3.2.11) lib/abstract_controller/base.rb:121:in `process'
  actionpack (3.2.11) lib/abstract_controller/rendering.rb:45:in `process'
  actionpack (3.2.11) lib/action_controller/metal.rb:203:in `dispatch'
  actionpack (3.2.11) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'
  actionpack (3.2.11) lib/action_controller/metal.rb:246:in `block in action'
  actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `call'
  actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:73:in `dispatch'
  actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:36:in `call'
  actionpack (3.2.11) lib/action_dispatch/routing/mapper.rb:42:in `call'
  journey (1.0.4) lib/journey/router.rb:68:in `block in call'
  journey (1.0.4) lib/journey/router.rb:56:in `each'
  journey (1.0.4) lib/journey/router.rb:56:in `call'
  actionpack (3.2.11) lib/action_dispatch/routing/route_set.rb:601:in `call'
  omniauth (1.1.3) lib/omniauth/strategy.rb:177:in `call!'
  omniauth (1.1.3) lib/omniauth/strategy.rb:157:in `call'
  meta_request (0.2.3) lib/meta_request/middlewares/app_request_handler.rb:11:in `call'
  rack-contrib (1.1.0) lib/rack/contrib/response_headers.rb:17:in `call'
  meta_request (0.2.3) lib/meta_request/middlewares/headers.rb:16:in `call'
  meta_request (0.2.3) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
  warden (1.2.1) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.1) lib/warden/manager.rb:34:in `catch'
  warden (1.2.1) lib/warden/manager.rb:34:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call'
  rack (1.4.5) lib/rack/etag.rb:23:in `call'
  rack (1.4.5) lib/rack/conditionalget.rb:35:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/head.rb:14:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/params_parser.rb:21:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/flash.rb:242:in `call'
  rack (1.4.5) lib/rack/session/abstract/id.rb:210:in `context'
  rack (1.4.5) lib/rack/session/abstract/id.rb:205:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/cookies.rb:341:in `call'
  activerecord (3.2.11) lib/active_record/query_cache.rb:64:in `call'
  activerecord (3.2.11) lib/active_record/connection_adapters/abstract/connection_pool.rb:479:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:28:in `block in call'
  activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `_run__111743413__call__128269692__callbacks'
  activesupport (3.2.11) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.11) lib/active_support/callbacks.rb:385:in `_run_call_callbacks'
  activesupport (3.2.11) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.11) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/reloader.rb:65:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/remote_ip.rb:31:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/debug_exceptions.rb:16:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
  railties (3.2.11) lib/rails/rack/logger.rb:32:in `call_app'
  railties (3.2.11) lib/rails/rack/logger.rb:16:in `block in call'
  activesupport (3.2.11) lib/active_support/tagged_logging.rb:22:in `tagged'
  railties (3.2.11) lib/rails/rack/logger.rb:16:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/request_id.rb:22:in `call'
  rack (1.4.5) lib/rack/methodoverride.rb:21:in `call'
  rack (1.4.5) lib/rack/runtime.rb:17:in `call'
  activesupport (3.2.11) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
  rack (1.4.5) lib/rack/lock.rb:15:in `call'
  actionpack (3.2.11) lib/action_dispatch/middleware/static.rb:62:in `call'
  railties (3.2.11) lib/rails/engine.rb:479:in `call'
  railties (3.2.11) lib/rails/application.rb:223:in `call'
  rack (1.4.5) lib/rack/content_length.rb:14:in `call'
  railties (3.2.11) lib/rails/rack/log_tailer.rb:17:in `call'
  rack (1.4.5) lib/rack/handler/webrick.rb:59:in `service'
  /home/Pavilion/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
  /home/Pavilion/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
  /home/Pavilion/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'

...

Started POST "/users" for 127.0.0.1 at 2013-04-18 15:15:23 -0400
Processing by Devise::RegistrationsController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"xxxxx=", "user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
  [1m[36m (1.0ms)[0m  [1mBEGIN[0m
  [1m[35mUser Exists (0.0ms)[0m  SELECT 1 AS one FROM "users" WHERE "users"."email" = '[email protected]' LIMIT 1
  [1m[36mUser Load (0.0ms)[0m  [1mSELECT "users".* FROM "users" WHERE "users"."confirmation_token" = 'xxxxx' LIMIT 1[0m
  [1m[35mSQL (1.0ms)[0m  INSERT INTO "users" ("confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "description", "email", "encrypted_password", "first_name", "headline", "image", "industry", "last_name", "last_sign_in_at", "last_sign_in_ip", "location", "name", "nickname", "provider", "public_profile", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "uid", "unconfirmed_email", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28) RETURNING "id"  [["confirmation_sent_at", Thu, 18 Apr 2013 19:15:23 UTC +00:00], ["confirmation_token", "xxxxx"], ["confirmed_at", nil], ["created_at", Thu, 18 Apr 2013 19:15:23 UTC +00:00], ["current_sign_in_at", nil], ["current_sign_in_ip", nil], ["description", nil], ["email", "[email protected]"], ["encrypted_password", "xxxxx"], ["first_name", nil], ["headline", nil], ["image", nil], ["industry", nil], ["last_name", nil], ["last_sign_in_at", nil], ["last_sign_in_ip", nil], ["location", nil], ["name", nil], ["nickname", nil], ["provider", nil], ["public_profile", nil], ["remember_created_at", nil], ["reset_password_sent_at", nil], ["reset_password_token", nil], ["sign_in_count", 0], ["uid", nil], ["unconfirmed_email", nil], ["updated_at", Thu, 18 Apr 2013 19:15:23 UTC +00:00]]
Errno::EBADF: Bad file descriptor - rb_thread_select(): INSERT INTO "users" ("confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "description", "email", "encrypted_password", "first_name", "headline", "image", "industry", "last_name", "last_sign_in_at", "last_sign_in_ip", "location", "name", "nickname", "provider", "public_profile", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "uid", "unconfirmed_email", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28) RETURNING "id"
  [1m[36m (1.0ms)[0m  [1mROLLBACK[0m
Completed 500 Internal Server Error in 469ms

ActiveRecord::StatementInvalid (Errno::EBADF: Bad file descriptor - rb_thread_select(): INSERT INTO "users" ("confirmation_sent_at", "confirmation_token", "confirmed_at", "created_at", "current_sign_in_at", "current_sign_in_ip", "description", "email", "encrypted_password", "first_name", "headline", "image", "industry", "last_name", "last_sign_in_at", "last_sign_in_ip", "location", "name", "nickname", "provider", "public_profile", "remember_created_at", "reset_password_sent_at", "reset_password_token", "sign_in_count", "uid", "unconfirmed_email", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27, $28) RETURNING "id"):


...

Started DELETE "/users" for 127.0.0.1 at 2013-04-18 15:17:25 -0400
Processing by Devise::RegistrationsController#destroy as HTML
  Parameters: {"authenticity_token"=>"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="}
  [1m[35mUser Load (1.0ms)[0m  SELECT "users".* FROM "users" WHERE "users"."id" = 57 LIMIT 1
  [1m[36m (3.0ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (0.0ms)[0m  DELETE FROM "users" WHERE "users"."id" = $1  [["id", 57]]
Errno::EBADF: Bad file descriptor - rb_thread_select(): DELETE FROM "users" WHERE "users"."id" = $1
  [1m[36m (1.0ms)[0m  [1mROLLBACK[0m
Completed 500 Internal Server Error in 7ms

ActiveRecord::StatementInvalid (Errno::EBADF: Bad file descriptor - rb_thread_select(): DELETE FROM "users" WHERE "users"."id" = $1):

Why can't I get full functionality from my newly created development database while all is working fine in production? I understand from my research that it is important to have the same databases in development and production.

I am new and learning ruby, rails, databases and postgresql. stackoverflow has been extremely helpful.

share|improve this question
You have no host in your database.yml? What does ActiveRecord::Base.connection.execute('select count(*) from users') have to say from the Rails console? – mu is too short Apr 17 at 22:49
I did have host: localhost:5432 in database.yml but Rails Server could not resolve and would not start. Rails Console returns (1.0ms) select count(*) from users => #<PG::Result:0x816782bc> I am able to pull users from the database from the console by User.find_by_email("[email protected]") – Matt Singer Apr 24 at 14:38

1 Answer

I am able to interact directly with the database either through the pgAdmin GUI or the psql > mydatabase command line.

So why keep fiddling with the database? That's not the part that's causing you problems.

This:

EBADF: Bad file descriptor

suggests a problem connecting, or perhaps even a problem with the database connection library itself.

Turn connection logging on in PostgreSQL. Turn logging on in your application. Make sure you can connect before looking further.

share|improve this answer
I updated my question to include both postgresql and application logging. I see the connections to the database which are successful and no errors in the postgresql log. The application log shows the errors. I see the bad file descriptor mention but not sure what that means and "id=$1 which is not correct, possibly my problem? – Matt Singer Apr 18 at 21:03
The $1 is a placeholder for your actual value, e.g 57. That's fine. The file descriptor error from rb_thread_select() is where the thread-locking is being done while the external library (libpq) talks to the database. Why it's failing, I can't say. Something is broken on your Ruby installation though. – Richard Huxton Apr 18 at 21:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.