How to use connection string with sequel for postgresql - ruby

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

Related

Specify elasticsearch username/password with Elasticsearch-PHP ClientBuilder

I have activated default "elastic" user and set a password for that user. I am using elasticsearch-php to connect and query my elasticsearch. It was working good, but after activating the password I cannot connect using my previous code.
While looking for providing authentication information with the ClientBuilder, I only get configurations regarding ssh connection. Did not find anything how can I use my elasticsearch username and password with the connection.
$hosts = ['127.0.0.1:9200'];
$es_user = 'elastic';
$es_pass = 'MY_PASS';
$elasticsearch = Elasticsearch\ClientBuilder::create()->setHosts($hosts)->build();
I am wondering how can I use my username/password in the connection above.
You can do it with an extended host configuration like this:
$hosts = [
[
'host' => '127.0.0.1',
'port' => '9200',
'user' => 'elastic',
'pass' => 'MY_PASS'
]
];
$elasticsearch = Elasticsearch\ClientBuilder::create()
->setHosts($hosts)
->build();

Binding to LDAP with multiple certs. in ruby

I'm using https://github.com/ruby-ldap/ruby-net-ldap gem to connect/bind to LDAP
treebase = "ou=xxxxxx,dc=xxxxx,dc=xx"
credentials = {
:username => "myusername,ou=xxxxxx,dc=xxxxx,dc=xx",
:password => "password"
}
encryption = {
:method => :simple_tls,
:tls_options => { :ca_path => "folder/Chain/pem_folder"}
}
# pem_folder = folder that contains multiple pem files
ldap = Net::LDAP.new :host => "hostname.example.com",
:port => 636,
:encryption => encryption,
:base => treebase,
:auth => credentials
ldap.bind
The above code results in
ruby-2.2.2/gems/net-ldap-0.12.1/lib/net/ldap/connection.rb:47:in open_connection': SSL_connect returned=1 errno=0 state=error: certificate verify failed (Net::LDAP::Error)
I have a feeling it has something to do with the multiple certs but not sure.
What worked for me was
Make sure you are connected to VPN if needed to be on the network
follow the instruction here specially the part about rehashing with c_rehash
my username was also missing uid= before which was causing failed binding
Stackoverflow also helped but I used Figaro Gem instead

Ruby Sinatra App Can't Connect to Local Postgres

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.

How to use jabber-bot to connect to chat server

we have a private chat server running on one of the servers we own. for example: company.server.com. We connect to this with our chat clients on port 5223.
How can I get Jabber-bot to connect to this chat? What if I want to connect to a particular room?
Below is what I have but it seems to just hang and do nothing. I believe there should be a property where I should provide the name of the server I'm connecting to but I can't find that property anyplace.
require 'rubygems'
require './jabber/bot'
# Create a public Jabber::Bot
config = {
:jabber_id => 'name#mycompany.com',
:password => 'mypassword',
:master => 'name#mycompany.com',
:presence => :chat,
}
bot = Jabber::Bot.new(config)
# Give your bot a private command, 'rand'
bot.add_command(
:syntax => 'rand',
:description => 'Produce a random number from 0 to 10',
:regex => /^rand$/
) { rand(10).to_s }
# Bring your new bot to life
bot.connect
session = Jabber::Session.bind('account#host/resource', 'password')

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