I am new to ruby. What is the best approach to make a simple postgresql DB via a ruby script? Can I use active record for this (not sure if it can be used via a ruby script)? or there are better alternatives?
I appreciate if you could help me with a ruby code snippet, or redirect me through a link.
Thanks.
I agree with AlexFranco that you can easily achieve that with the pg gem.
However, when you read how to use the pg gem you might wonder which database you should connect to.. after all you want don't have a database yet, if you had you wouldn't want to create one..
Turns out there is a master-db called postgres. So here's a minimal working example:
require 'pg'
conn = PG.connect(dbname: 'postgres')
conn.exec("CREATE DATABASE foo")
See the postgresql documentation on options for the create database statement. More info on how to use the pg gem can be found in the docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With