Skip to content

Instantly share code, notes, and snippets.

@Winslett
Last active August 29, 2015 14:24
Show Gist options
  • Save Winslett/8e0ab9066d4888ee760e to your computer and use it in GitHub Desktop.
Save Winslett/8e0ab9066d4888ee760e to your computer and use it in GitHub Desktop.
ruby-pg connection retry from PG::UnableToSend
uri = URI.parse(ENV['POSTGRES_URL'])
$query_postgres = lambda do |sql|
begin
$postgres ||= PG.connect(uri.hostname, uri.port, nil, nil, uri.path[1..-1], uri.user, uri.password)
$postgres.exec(sql)
rescue PG::UnableToSend
$postgres = nil
retry
end
end
result = $query_postgres.call("SELECT * FROM accounts")
result.each do |row|
row.each do |key, value|
puts [key, value].join("=")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment