Reputation: 100010
I am relatively new to Rails and web development. This is a simple conceptual question. Say you are developing the rails code on your local machine and then you push your code to a server. Is your rails code stored in the same place as the database for your application by default? How does rails know where to put the database on disk when you first push to a remote server? There must be a default, and how do you change the default?
Furthermore, when you first create an app and run the app locally, there must be a local database, so what exactly happens when you push the code to a server?
Would you ever put the database on a different server than where the rails code lies?
Upvotes: 1
Views: 3522
Reputation: 76774
Rails has nothing to do with where your db is stored
The fact it handles a local DB (for development) is co-incidental. The db needs to be stored in the most efficient, fastest & most scalable environment; which may, or may not, be local depending on your setup
Heroku
As you've cited Heroku in your tags, you need to know Heroku just uses Amazon AWS services, meaning your db will be stored in the same data center, but on a different computing platform
The way to access these db's is through a custom URL (check heroku config
- DATABASE_URL
to see where your Heroku db is stored). This means Rails can use either localhost (127.0.0.1:3000
) or an external IP to connect to the db
The config/database.yml
file holds all the connection information for your app:
development:
adapter: sqlite3
database: db/development.sqlite3
pool: 5
timeout: 5000
test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
timeout: 5000
production:
adapter: mysql
encoding: utf8
database: your_db
username: root
password: your_pass
socket: /tmp/mysql.sock
host: your_db_ip #defaults to 127.0.0.1
port: 3306
The 3 environments allow you to set different databases for development, production etc. Heroku is by default the "production" environment, although you can set it to the staging environment too
As long as your connection details are correct, you should be able to connect to any system
Upvotes: 2
Reputation: 3467
If you're using SQLite3 (which is default but not recommended for production use) it will be stored in the db
folder. In all other cases the database will be a separate, independent service the Rails app talks to, for instance a Postgresql server.
It's completely possible to use SQLite3 for development and a database server in production. It's configured in config/database.yml
. Take a look at the defaults, it's pretty self explanatory. However, I'd recommend using the same type of database for both environments since it's easy for beginners to forget to run your database migrations when using SQLite (SQLite creates everything on the fly if it doesn't exist while Rails uses a series of scripts to add and remove tables/columns).
Larger applications would definitely put the database on another server than the app itself. This can for instance allow scaling the application server and the database server independently or let you quickly add a new database server if your current one fails.
I recommend you read the getting started guide to get to know the basics of Rails.
Upvotes: 3
Reputation: 5221
Where the actual database lives is dependent on the database server. For example, a mysql database maybe on a different directory than a pgsql. Rails knows where to find the data through the database.yml
.
This file specifies how your adapter should connect to the database server instance running on the machine. Rails generally follows a client server model. i.e. it sends queries to your database server instance and gets the results back.
Note that the database server instance is a process completely independent of rails. If you will use python, you can still connect to the same database by providing the correct port number and address. The database instance determines where your data actually gets stored.
Similarly, you can define different setting for multiple environments in database.yml
. When you move to production, rails is fired generally under production env. It will look for instructions on how to connect to database under the production heading and make connections from there. As such, dev and production databases are independent.
Upvotes: 1