ruby-oci8: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor - ruby

I tried to connect to Oracle DB with the latest oracle_enhanced, ruby-oci8 gems on a Windows 7 x64 PC and it failed as follows:
$ ruby oracle_db_connection.rb
oci8.c:654:in oci8lib_230.so: ORA-12514: TNS:listener does not currently know of service requested in connect descriptor (OCIError)
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/ruby-oci8-2.2.2-x64-mingw32/lib/oci8/oci8.rb:142:in `initialize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:333:in `new'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:333:in `new_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:393:in `initialize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:26:in `new'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/oci_connection.rb:26:in `initialize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/connection.rb:9:in `new'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced/connection.rb:9:in `create'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-oracle_enhanced-adapter-1.6.7/lib/active_record/connection_adapters/oracle_enhanced_adapter.rb:156:in `oracle_enhanced_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
from C:/Ruby23-x64/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
from C:/Ruby23-x64/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:113:in `retrieve_connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_handling.rb:87:in `connection'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/model_schema.rb:230:in `table_exists?'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/primary_key.rb:97:in `get_primary_key'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/primary_key.rb:85:in `reset_primary_key'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/attribute_methods/primary_key.rb:73:in `primary_key'
from C:in `primary_key'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:493:in `find_nth_with_limit'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:484:in `find_nth'
from C:/Ruby23-x64/lib/ruby/gems/2.3.0/gems/activerecord-4.2.6/lib/active_record/relation/finder_methods.rb:127:in `first'
from C:in `first'
from oracle_db_connection.rb:30:in `<main>'
I installed Oracle instant client by downloading and unzipped it to
**C:\Program Files\Oracle\instantclient_12_1**
I added the environment variable TNS_ADMIN pointing to C:\Program Files\Oracle\instantclient_12_1.
I added TNS_ADMIN to the Path.
When running the simple ruby script:
require 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'oracle_enhanced',
database: 'my_db_sid',
host: 'my_db_host',
port: 1531,
username: 'username',
password: 'password'
)
class Customer < ActiveRecord::Base
self.table_name = 'CUSTOMER'
end
puts "First: #{Customer.first}"
If I remove the call to the Customer table, I can inspect connection object:
connection = ActiveRecord::Base.establish_connection(
adapter: 'oracle_enhanced',
database: 'my_db_sid',
host: 'my_db_host',
port: 1531,
username: 'username',
password: 'password'
)
puts connection.inspect
#class Customer < ActiveRecord::Base
# self.table_name = 'CUSTOMER'
#end
#
#puts "First: #{Customer.first}"
Any idea on what is wrong here ? The script worked previousely fine on other PCs but with other DBs and environement. So the used settings should be fine.
Thank you.
Environment:
Windows 7 x 64 bits Ruby 2.3
activerecord-oracle_enhanced-adapter (1.6.7)
ruby-oci8 (2.2.2 x64-mingw32)

You need to use some different format of database section. Here's my config:
adapter: "oracle_enhanced"
username: "db_user"
database: "//my.host/my.sid"
password: "passw0rd"

I figured out how to connect to one of 2 databases. They have different settings, at least in Sqldeveloper v 4.1.3.20.
Here are the settings that worked for me (please note, it is out of Rails context, just a ruby file):
require 'active_record'
ActiveRecord::Base.establish_connection(
adapter: 'oracle_enhanced',
host: 'as specified in the field Host name of SQL developer',
port: 'as specified in the field Port of SQL developer',
database: 'as specified in the fiels Service name of SQL developer',
username: 'username',
password: 'userpassword'
)
class Department < ActiveRecord::Base
self.table_name = 'DEPARTMENTS'
end
puts "First found department: #{Department.first.inspect}"
The second database has different settings in SQL developer:
- host: some_host_url
- port: some port number
- SID: some SID value
The last soltion was to add the following settings:
HOST = "your host"
PORT = XXXX #your port number
SID = 'your SID'
ActiveRecord::Base.establish_connection(
adapter: 'oracle_enhanced',
database: "(DESCRIPTION=
(ADDRESS_LIST=(ADDRESS=(PROTOCOL=tcp)(HOST=#{HOST})(PORT=#{PORT})))
(CONNECT_DATA=(SID=#{SID}))
)",
username: 'user',
password: 'secret'
)
Hope this helps.

Related

PG::ConnectionBad: PQconsumeInput() SSL error: key values mismatch

When I open a certificate with OpenSSL::PKCS12, I lose the connection with my database and occur the error: PG::ConnectionBad: PQconsumeInput() SSL error: key values mismatch.
I'm doing this:
myuser#developer:~/myapp$ rails c
2.2.3 :001 > OpenSSL::PKCS12.new File.read('/local/to/mycert.pfx'), 'PASSWORD'
=> #<OpenSSL::PKCS12:0x000000072e6808 #key=#<OpenSSL::PKey::RSA:0x000000072e67e0>, #certificate=#<OpenSSL::X509::Certificate: subject=#<OpenSSL::X509::Name:0x000000072e6100>, issuer=#<OpenSSL::X509::Name:0x000000072e6128>, serial=#<OpenSSL::BN:0x000000072e6150>, not_before=2014-12-18 19:15:55 UTC, not_after=2015-12-18 19:15:55 UTC>,
#ca_certs=[#<OpenSSL::X509::Certificate: subject=#<OpenSSL::X509::Name:0x000000072e5c78>, issuer=#<OpenSSL::X509::Name:0x000000072e5ca0>, serial=#<OpenSSL::BN:0x000000072e5cc8>, not_before=2014-08-04 18:38:36 UTC, not_after=2021-10-11 18:38:36 UTC>, #<OpenSSL::X509::Certificate: subject=#<OpenSSL::X509::Name:0x000000072e5818>, issuer=#<OpenSSL::X509::Name:0x000000072e5840>, serial=#<OpenSSL::BN:0x000000072e5868>, not_before=2011-10-21 12:16:29 UTC, not_after=2021-10-21 12:16:29 UTC>, #<OpenSSL::X509::Certificate: subject=#<OpenSSL::X509::Name:0x000000072e52f0>, issuer=#<OpenSSL::X509::Name:0x000000072e5318>, serial=#<OpenSSL::BN:0x000000072e5340>, not_before=2010-06-21 19:04:57 UTC, not_after=2023-06-21 19:04:57 UTC>]>
After this I lose the connection:
2.2.3 :002 > Company.last
PG::ConnectionBad: PQconsumeInput() SSL error: key values mismatch
: SELECT tablename
FROM pg_tables
WHERE schemaname = ANY (current_schemas(false))
ActiveRecord::StatementInvalid: PG::ConnectionBad: PQconsumeInput() SSL error: key values mismatch
: SELECT tablename
FROM pg_tables
WHERE schemaname = ANY (current_schemas(false))
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:147:in `async_exec'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:147:in `block in query'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activesupport-4.2.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:146:in `query'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/postgresql/schema_statements.rb:91:in `tables'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/schema_cache.rb:90:in `prepare_tables'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/connection_adapters/schema_cache.rb:22:in `table_exists?'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/model_schema.rb:230:in `table_exists?'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/attribute_methods/primary_key.rb:97:in `get_primary_key'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/attribute_methods/primary_key.rb:85:in `reset_primary_key'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/attribute_methods/primary_key.rb:73:in `primary_key'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/attribute_methods/primary_key.rb:80:in `quoted_primary_key'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/delegation.rb:48:in `quoted_primary_key'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/query_methods.rb:1078:in `reverse_sql_order'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/query_methods.rb:852:in `reverse_order!'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/query_methods.rb:846:in `reverse_order'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/finder_methods.rb:511:in `find_last'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/relation/finder_methods.rb:160:in `last'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/activerecord-4.2.4/lib/active_record/querying.rb:3:in `last'
from (irb):2
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands/console.rb:110:in `start'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands/console.rb:9:in `start'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/myuser/.rvm/gems/ruby-2.2.3/gems/railties-4.2.4/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'2.2.3 :003 >
I perceive that this occurs when the method ca_certs is not null.
Ruby version: 2.2.3
Rails version: 4.2.4
gem 'pg', '0.18.3'
Has anyone had this problem?
I encountered this same problem today. After a long search I came across this thread. Seems like a bug in OpenSSL library/extension. Because opening a certificate and talking to a DB happen in a single thread, the connection gets screwed up. I still haven't figured out all details but a workaround is to open the certificate in it's own thread, like:
pkcs = nil
Thread.new { pkcs = OpenSSL::PKCS12.new File.read('/local/to/mycert.pfx'), 'PASSWORD' }.join
Hope that helps :)

Connect to existing Postgresql database with ActiveRecord without rails

I am not using rails, and I'm trying to use ActiveRecord to make it easier to work with an existing database. This is just a script so I have no database.yml or any other files. I've set up my database connection using
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'thehosthere',
database: 'management',
username: 'management_readonly',
password: '',
port: '5432'
)
I'm not really familiar with databases, so I'll just state what I know about this one. There are quite a few schemas. The schema I'm interested in is the management schema that has the table "host". I've created a class Host in the script like this:
class Host < ActiveRecord::Base
self.table_name = "host"
end
I'm then trying to pull all rows that match the criteria with this query and to store it into an array.
servers = Host.where(:realm => 'stage', :status => 'UP').pluck(:hostname)
but I am getting this error every single time.
ruby environments_are_okDev.rb
/usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec': PG::UndefinedTable: ERROR: relation "host" does not exist (ActiveRecord::StatementInvalid)
LINE 5: WHERE a.attrelid = '"host"'::regclass
^
: SELECT a.attname, format_type(a.atttypid, a.atttypmod),
pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid,
a.atttypmod
FROM pg_attribute a LEFT JOIN pg_attrdef d
ON a.attrelid = d.adrelid AND a.attnum = d.adnum
WHERE a.attrelid = '"host"'::regclass
AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec_no_cache'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:138:in `block in exec_query'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in `block in log'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in `log'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql_adapter.rb:915:in `column_definitions'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/postgresql/schema_statements.rb:174:in `columns'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:114:in `block in prepare_default_proc'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `yield'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `default'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/connection_adapters/schema_cache.rb:56:in `columns'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:208:in `columns'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/model_schema.rb:247:in `column_names'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:60:in `block in method_missing'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation.rb:270:in `scoping'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/delegation.rb:60:in `method_missing'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:152:in `block in pluck'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:151:in `map!'
from /usr/local/rvm/gems/ruby-1.9.3-p448/gems/activerecord-4.0.0/lib/active_record/relation/calculations.rb:151:in `pluck'
from environments_are_okDev.rb:51:in `run'
from environments_are_okDev.rb:97:in `<main>'
I don't really understand why this is happening, so I tried to see what the database looks like with this command ActiveRecord::Base.connection.tables but all I got was an empty array. I'm not sure what could be happening. I'm obviously doing something wrong, I just can't figure out what. Keep in mind, this is all in a single file script.
Edit:
So I can see the schemas if I use ActiveRecord::Base.connection.schema_names. This gives me an array => ["db_stats", "management", "management_audit", "public"] Using pgadmin, I know that the table I want is in management, so how do I access that?
Try adding schema_search_path when you call establish_connection:
ActiveRecord::Base.establish_connection(
adapter: 'postgresql',
host: 'thehosthere',
database: 'management',
username: 'management_readonly',
password: '',
port: 5432,
schema_search_path: 'management' # <-- ???
)

Debugging mail delivery in IronWorker

I'm trying to use the mail gem to send an email from an IronWorker task. Running the script works on my local system (OS X), but fails when run on IronWorker with the same parameters. I'm new to IronWorker and the mail gem, so I'm at a loss of where to start debugging. Here's the text of the error message.
/task/email.rb:5:in `block in <top (required)>': undefined method `[]' for nil:NilClass (NoMethodError)
from /task/__gems__/gems/mail-2.4.4/lib/mail/mail.rb:106:in `instance_eval'
from /task/__gems__/gems/mail-2.4.4/lib/mail/mail.rb:106:in `defaults'
from /task/email.rb:3:in `<top (required)>'
from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/lib/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from __runner__.rb:213:in `<main>'
And here's my code:
require 'mail'
Mail.defaults do
delivery_method :smtp, {
address: params['address'],
port: 587,
domain: params['domain'],
user_name: params['username'],
password: params['password'],
authentication: 'plain',
enable_starttls_auto: true
}
end
Mail.deliver do
to "#{params['to_name']} <#{params['to_address']}>"
from "#{params['from_name']} <#{params['from_address']}>"
subject "Mail test"
text_part do
body params['text']
end
html_part do
content_type 'text/html; charset=UTF-8'
body params['html']
end
end
Note that I was getting this error before locally, but it was caused by using username in the defaults instead of user_name. Any ideas?
I'm not sure but looks like 'params' is nil, could you try to inspect it before using in. It could happened if you're using in your worker separate namespaces (like worker body inside class)

(Ruby) ActiveRecord with PG try to connect with local user to remote server

I made a simple-coding-ruby-program for get backups from remote server, and I made a db in postgresql for save information and schedule backups.
The connect with DB was done with ActiveRecord, I configured for access a internal DB (in other server), but when I try to connect to remote db, i get this message:
/usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:968:in `initialize': FATAL: no existe el rol <<mi_user_name>> (PGError)
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:968:in `connect'
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:968:in `connect'
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:217:in `initialize'
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:37:in `new'
from /usr/lib/ruby/1.8/active_record/connection_adapters/postgresql_adapter.rb:37:in `postgresql_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:223:in `send'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:223:in `new_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:245:in `checkout_new_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:188:in `checkout'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:184:in `loop'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:184:in `checkout'
from /usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:183:in `checkout'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:98:in `connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:326:in `retrieve_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:123:in `retrieve_connection'
from /usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:115:in `connection'
from /usr/lib/ruby/1.8/active_record/base.rb:3113:in `quoted_table_name'
from /usr/lib/ruby/1.8/active_record/base.rb:1684:in `construct_finder_sql'
from /usr/lib/ruby/1.8/active_record/base.rb:1548:in `find_every'
from /usr/lib/ruby/1.8/active_record/base.rb:1505:in `find_initial'
from /usr/lib/ruby/1.8/active_record/base.rb:613:in `find'
from main.rb:84:in `main'
from main.rb:126
PG try to connect to remote db with my local user, in the declaration of ActiveRecord I set the parameters:
require 'active_record'
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => "xxx.xxx.x.101",
:port => 5432,
:database => "VB_DB",
:user => "pg_user",
:password => "blahblah"
)
I working with Ruby 1.8.7.
Any idea about this?
thank for reading, and help me.
Grettings.
EDIT
I found the solution:
ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => "xxx.xxx.x.101",
:port => 5432,
:database => "VB_DB",
:username => "pg_user",
:password => "blahblah"
)
I used :user and must be :username.
Shame on me.
From the error message, it appears that Postgres doesn't think your user exists in the database.
A good first step to troubleshoot this is to try connecting without involving Ruby at all. Since the 'psql' command-line client uses the same underlying library (assuming you're using the 'pg' gem to connect), in general if you can connect using that, you should be okay to connect from Ruby as well.
Try this:
psql -h xxx.xxx.x.101 -U pg_user -W -l
If this shows you the same error (no existe el rol <<mi_user_name>>), then you'll need to confirm that your user exists, and create it.
If it shows you a list of databases, we'll have to try something else.

Do I need a connection to use ActiveRecordObject.new?

Can you create and initialize an ActiveRecord object like this?
exercise = Exercise.new
exercise.name = "foo"
exercise.description = "bar"
exercise.unit_id = 123
I am trying to do this and I keep getting
ActiveRecord::ConnectionNotEstablished - ActiveRecord::ConnectionNotEstablished:
/usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_pool.rb:326:in `retrieve_connection'
/usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:123:in `retrieve_connection'
/usr/lib/ruby/1.8/active_record/connection_adapters/abstract/connection_specification.rb:115:in `connection'
/usr/lib/ruby/1.8/active_record/base.rb:1271:in `columns'
/usr/lib/ruby/1.8/active_record/base.rb:3014:in `attributes_from_column_definition_without_lock'
/usr/lib/ruby/1.8/active_record/locking/optimistic.rb:55:in `attributes_from_column_definition'
/usr/lib/ruby/1.8/active_record/base.rb:2434:in `initialize'
main.rb:14:in `new'
I just want to create and initialize the object then call a .save method. If I am going about this incorrectly, please let me know.
Short Answer: Yes, you need a connection!
You cannot (generally) use ActiveRecord unless there is a connection.
The set of attributes on the object is determined by the columns on the table. Which means you can't use the object unless you have the connection.
In any case what would happen after calling exercise.save if there is no connection?
So if you only need the validation, you can reuse ActiveSupport::Validation on a transient class.
This would imply that Rails cannot connect to your database. Ensure your database server is on (if applicable), and that your config/database.yml file is correct.
Try this at the top of your code
ActiveRecord::Base.establish_connection(
:adapter => "mysql",
:host => "localhost",
:username => "<your database username>",
:password => "<your database password>",
:database => "<your database name>"
)
The above code should establish an ActiveRecord database connection to your database. You can check out the manual for it here http://api.rubyonrails.org/classes/ActiveRecord/Base.html#method-c-establish_connection. Good luck!

Resources