Skip to content

Instantly share code, notes, and snippets.

@adamwiggins
Created June 23, 2010 23:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamwiggins/450738 to your computer and use it in GitHub Desktop.
Save adamwiggins/450738 to your computer and use it in GitHub Desktop.
require 'system_timer'
require 'uri'
bad_url = 'postgres://some:user@1.2.3.4/mydb'
good_url = 'postgres://postgres@localhost/template1'
uri = URI.parse(good_url)
success = nil
SystemTimer.timeout(1) do
begin
result = `PGPASSWORD=#{uri.password} psql -h #{uri.host} -U #{uri.user} #{uri.path.gsub(/^\//, '')} -c 'select 1' 2>& 1`
success = result.match(/\(1 row\)/)
rescue Timeout::Error
success = false
end
end
puts "server is #{success ? 'up' : 'down'}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment