I’m developing a small Ruby application that I’d like to work with both PostgreSQL and MySQL.
It seems like Ruby/DBI is the most straightforward way to do this; I don’t want to install a heavyweight ORM like ActiveRecord or something. Unfortunately, it seems like Ruby/DBI makes it simple to connect if you already have a database but I can’t figure out how to connect to a server without a DB and issue the CREATE DATABASE command.
So far I have this:
dbh = DBI.connect("DBI::Pg", "username", "password")
but I get this error when I try and connect: Invalid Data Source Name. Any idea of what I can do?
Also, if I can get a ‘databaseless handle’ I was wondering if there was a DBI method to select_db and drop into a database to execute further queries. That’s secondary though and I can live without it.
You should have a
postgresdatabase so you could try:And then execute your
CREATE DATABASEfrom there. Also, the first argument toconnectis supposed to be:Where
Driveris the database driver (case sensitive) anddatabase_conn_argsis usually the database name.