Ruby Sinatra App Can't Connect to Local Postgres - ruby

I recently made a small wrapper in Sinatra. I had it working locally using SQLite, and I decided to switch to Postgres for the eventual move to Heroku.
However, I can't get my app to connect to my local postgres db. I'm running Windows 7 64-bit.
Here is database.yml:
development:
host: localhost
adapter: postgresql
database: postgres
username:
password:
Here is environments.rb:
configure :production, :development do
db = URI.parse(ENV['DATABASE_URL'] || 'postgres://localhost/postgres')
ActiveRecord::Base.establish_connection(
:adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme,
:host => db.host,
:username => db.user,
:password => db.password,
:database => db.path[1..-1],
:encoding => 'utf8'
)
end
This is the result when I try to rake:
Could not load 'active_record/connection_adapters/postgresql_adapter'. Make sure that the adapter in config/database.yml is valid. If you use an adapter other than 'mysql', 'mysql2', 'postgresql' or 'sqlite3' add the necessary adapter gem to the Gemfile.
I can connect to the db via psql and localhost just fine.

Related

How to use connection string with sequel for postgresql

I wanted to connect to postgresql database using connection string in form of
host=localhost user=x password=y dbname=z
this works fine when passed to psql
$ psql "host=localhost user=x password=y dbname=z"
and when passed to the PG gem
> PG.connect("host=localhost user=x password=y dbname=z")
however it fails when I try to pass it into sequel
> Sequel.postgres(constr)
Sequel::DatabaseConnectionError: PG::ConnectionBad: could not connect to server: No such file or directory
How can I make sequel connect to my database?
You could use a string with the connection information, for example:
conn_str = 'postgres://localhost/database_name?user=user&password=password'
DB = Sequel.connect(conn_str)
Check the Sequel's documentation of how to open connections with a hash
DB = Sequel.connect(
adapter: 'postgres',
host: 'localhost',
database: 'database_name',
user: 'database_user',
password: 'password'
)
Then you should be able to run:
DB[:table_name].where(:attribute => 5000..10000)
Or you could use the postgres method directly:
DB = Sequel.postgres('my_db', :user => 'user', :password => 'password', :host => 'localhost')
Check devhints open connection shortcuts

How to use Net::LDAP with JRuby

I am using Ruby and JRuby on a Ubuntu machine through RVM.
OpenLDAP is connecting and doing the CRUD operation through Ruby code with the Net::LDAP gem perfectly.
Here is the code:
require 'rubygems'
require 'net/ldap'
ldap = Net::LDAP.new :host => 'locahost',
:port => 389,
:async => 1,
:keepalive => 1,
:auth => {
:method => :simple,
:username => "cn=admin,dc=test,dc=com",
:password => "secret"
}
puts ldap.bind
When I execute the same code with JRuby, it throws an error:
$ jruby ldap_test.rb
ArgumentError: unsupported protocol family `__UNKNOWN_CONSTANT__'
initialize at org/jruby/ext/socket/RubySocket.java:188
new at org/jruby/RubyIO.java:847

Getting email to work on openshift ruby application

I can't seem to get email for password recovery using devise to work on my openshift app. I'm using Rails 4.0.2 and Ruby 1.9.3. I've tried the following in production.rb :
config.action_mailer.default_url_options = { :host => 'mydomain.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:port => 25,
:address => 'smtp.mailgun.org',
:user_name => 'postmaster#domain.com',
:password => '[password]',
:domain => 'mydomain.com.mailgun.domain',
:authentication => :plain,
}
I've also done settings required for google use and both worked fine in development on local. Also tried ports 465, 587, as described here. Sending mail unfortunately still isn't performed on production. The app just throws an error with nothing in the logs.

Specify complex password in Datamapper.setup

I m trying to use Datamapper as ORM for my Ruby/Padrino application. To setup db connection, in databse.rb, I have:
when :development then DataMapper.setup(:default, "mysql://db_user:dsfsdfs#localhost/my_app_development")
This work fine. But my requirement is to have complex password, something like:
when :development then DataMapper.setup(:default, "mysql://db_user:Passw0rd#13#localhost/my_app_development")
This doesn't work and I get error:
ruby/1.9.1/gems/addressable-2.2.8/lib/addressable/uri.rb:1179:in `port=': Invalid port number: "Passw0rd" (Addressable::URI::InvalidURIError)
I figured out that the '#' character is problem. Then I attempted doing this:
DataMapper.setup(:default,
{
:adapter => "mysql",
:database => "my_app_development",
:username => "db_user",
:password => "Passw0rd#13",
:host => "localhost"
})
When I do this, it seems like DM is ignoring the hash altogether; when I run
padrino rake db:create
It tries to connect as current logged in user, rather than the username specified here. Solution?
I suppose you still need to have to declare it like following, in case you are not
when :development then
DataMapper.setup(:default,
{
:adapter => "mysql",
:database => "my_app_development",
:username => "db_user",
:password => "Passw0rd#13",
:host => "localhost"
})
when :production then
# rest of your code
end
Got it - the key :username should be :user. Basically, the hash should contains keys as understood by Addressable::URI

attempting to connect to sybase but resolving to mssql adapter

require 'rubygems'
require 'jdbc/jtds'
require 'activerecord-jdbc-adapter'
config = {
:username => 'me',
:password => 'mypass',
:adapter => 'jdbc',
:driver => 'net.sourceforge.jtds.jdbc.Driver',
:url => 'jdbc:jtds:sybase://myserver:1234/mydb'
}
ActiveRecord::Base.establish_connection( config )
ActiveRecord::Base.connection.class
# => ActiveRecord::ConnectionAdapters::MssqlJdbcConnection
I realise that MSSQL and Sybase have a shared history, but is it correct that I get this adapter when connecting to Sybase?
My problem is that types are not being mapped to ruby type correctly. e.g. floats and, decimals are being translated to String.
Gems:
activerecord-jdbc-adapter 1.1.3
jdbc-jtds 1.2.5
ActiveRecord will resolved the JDBC adapter by adding the
:dialect => 'sybase'
...config option

Resources