Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby - best approach to create a postgresql DB

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.

like image 356
newbi Avatar asked Jul 05 '26 10:07

newbi


1 Answers

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.

like image 179
kaikuchn Avatar answered Jul 07 '26 07:07

kaikuchn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!