Skip to content

Instantly share code, notes, and snippets.

@almokhtarbr
Created October 13, 2021 12:34
Show Gist options
  • Save almokhtarbr/8d73d15ff5093bf79a138cbac286f5ce to your computer and use it in GitHub Desktop.
Save almokhtarbr/8d73d15ff5093bf79a138cbac286f5ce to your computer and use it in GitHub Desktop.
gem 'pg'
require 'pg'
def with_db
db = PG.connect(
dbname: 'dbname',
user: 'user',
password: 'password'
)
begin
yield db
ensure
db.close
end
end
sql = File.open('path/to/your_sql.sql', 'rb') { |file| file.read }
with_db do |db|
begin
db.exec(sql)
rescue PG::Error
#####
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment