1

Is it possible to create a database and tables in runtime, in a Rails app?

For example: the server receives a request with some data. Then, it should create a database in PostgreSQL, and create also some tables in that new DB. Is this possible?

2 Answers 2

0

Yes, Its possible. Perhaps not recommended.

Sign up to request clarification or add additional context in comments.

1 Comment

can you provide more information about why it is not recommended?
0

Using the Sequel gem (http://sequel.jeremyevans.net/) it is possible to create databases in runtime, running an SQL script.

I've tried it only with a PostgreSQL database, not sure if it is possible with others.

For a PostgreSQL database

Connect to the PostgreSQL server

To connect to the PostgreSQL server you always have to define a database to connect to. Use the 'postgres' database, a default database created by the PostgreSQL server (more info: Default database named postgres on Postgresql server).

db = Sequel.postgres('postgres', user, password, host, port)

Run the SQL script to create a database

db.run "CREATE DATABASE my_new_db"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.