Simply add a line in /Gemfile that looks like this: gem "pg"Īs always, be sure to run bundle after adding a gem to the Gemfile: $ bundle Database.yml We'll install Postgres in this chapter, although you could just as easily use MySQL or another database type.įirst, we'll add the pg rubygem to our app, which is the standard library for interacting with Postgres in Ruby. PostgreSQL (aka Postgres) is a great modern database that works well with Rails and has some useful advanced features. We'd like to switch to a more robust database for our production app. This is great for development but not great at handling lots of traffic. Right now our app is running using Sqlite3 for its database. The seed data you created in db/seeds.rb should already have been loaded when you ran rails db:setup. Now you should have PostgreSQL databases up and running locally to serve your app in development and test modes. This table is used to record your migrations to schemas and data. This will create development and test databases, set their owners to the user specified, and create "schema_migrations" tables in each. Once configured, your database.yml should contain something like this: default: &default For example, the test database is emptied and rebuilt every time you run Rails tests.įor each database, ensure that the username and password match the username and password you gave your Postgres user. We want each environment to have a separate database because they each use the database for different purposes. Next, change your test and development settings in database.yml to use your local Postgres instance, listing different databases for different environments: development, test, and production. To exit the postgres shell, enter \q at the prompt. To do this, we'll first open a psql console that allows us to issue Postgres commands: $ psqlĪlegscogs=# create role traveller createdb login password 'password' Next we'll create a Postgres user role for our app to use. To source that environment variable you may want to open a new terminal tab now, or else set it explicitly on the command line: $ TRAVELLER_PG_PASSWORD="my password" ~/.bash_profile export TRAVELLER_PG_PASSWORD="my password" bash_profile at your user's root directory, create one now: bash_profile, which runs every time we open a terminal to run commands. In order to make sure this environment variable is always available on our local machine, we'll add it to our. We'll want our app to use this password to authenticate when storing data, so we'll set our password in an environment variable that we can then access in our app. Next, we want to create a password to restrict who can use the database. When that's done, create a database "cluster" to use for our Rails app. Then start the postgres server: $ pg_ctl -D /usr/local/var/postgres -l logfile start $ brew install postgresĪfter the package is installed, you'll have to initialize postgres (remember you may need to open a new terminal window after installing postgres to get acces to the command line commands like initdb and pg_ctl ): $ initdb /usr/local/var/postgres To do this, you'll need to install Postgres locally. Our next step is to update all our other environments to use Postgres. We've already got Postgres working on our production server.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |