Bummer! This is just a preview. You need to be signed in with a Pro account to view the entire video.
Configuring Rails to Use PostgreSQL5:10 with Jay McGavren
Let's take an existing Rails app, and convert it from using SQLite to PostgreSQL.
In your terminal:
git clone https://github.com/treehouse-projects/rails-guestbook.git guestbook
Edit the project
# Replace: gem 'sqlite3' # With: gem 'pg', '~> 0.18'
In your terminal:
default: &default adapter: postgresql encoding: unicode pool: 5 development: <<: *default database: guestbook_development username: <YOUR_POSTGRESQL_USER_NAME> test: <<: *default database: guestbook_test username: <YOUR_POSTGRESQL_USER_NAME> production: <<: *default database: guestbook_production username: deploy
In your terminal:
Next we're going to need to update our rails app to use post press instead of 0:00 sequel line. 0:03 So we're going to need to first and use the postgres gem. 0:04 So I'm going to go here into our apps Gemfile and 0:09 you see here a line that says, Use sqlite3 as the database for Active Record. 0:12 We wanna use the pg gem instead. 0:16 So I'm gonna replace splite3 with pg, and 0:18 then we're gonna want a particular version. 0:20 So, I'm going to use the so-called twiddle wakka syntax, 0:23 which will lock it to a particular minor version. 0:27 So, I'm gonna use the latest version that's available 0:31 right now 0.18, save that. 0:36 And now, 0:40 here in the app directory I need to run bundle install to install the new gems. 0:41 Next I'm going to need to edit the database.yml file within the config 0:49 directory to tell it to connect to postgres instead of sqlite. 0:53 So let's delete all these comments here at the top that talk about sqlite and 0:58 here in the default set of options we're gonna set the adapter not to sqlite3, 1:04 but to postgresql. 1:10 It's going to want to know what encoding to save text as. 1:12 And we don't want it using ASCII we want it using unicode, so 1:17 that we can save characters from other languages. 1:20 We'll go and leave the pool config setting, and remove the time out here. 1:24 Now let's go on and 1:30 edit our particular environments which we'll use the default settings as a basis. 1:32 For our development database we want to use not this sqlite database, but 1:37 a postgres database named guestbook, 1:41 which is the name of our app, underscore development. 1:44 Unlike sqlite, postgres supports multiple users updating the same database, so 1:50 we need to tell it what username to expect updates to come from. 1:54 So I'm gonna add a username, setting and I'll use my developer account name, jay. 1:57 We'll do the same for the test environment. 2:05 Tell it not to look at a sqlite database file but 2:07 a postgres database named guestbook_test. 2:11 That's guestbook_test as opposed to guestbook development, 2:15 which it was in the development environment. 2:19 And again we're going to need a user name setting of jay, 2:23 which I'll just copy down from the development environment. 2:26 And last up is production, 2:30 which again will update it to say guestbook_production. 2:32 But for the production environment we don't want to use a username matching our 2:38 development name. 2:43 We're gonna want the username that's responsible for 2:44 maintenance of our application. 2:47 That's going to be the deploy user in this case. 2:48 Save that and we're done. 2:53 So we set our rails app up to use a postgres database ,now 2:55 let's actually create that database. 2:58 We're going to need to from our app to rectory run the command bin/rails 3:00 db:setup and what this will do, is it'll create both the development, and 3:05 the test databases, and it will load the schema into them. 3:10 Loading the scheme is actually going to be more efficient and safer than actually 3:14 running all our migrations since this is a brand new database. 3:17 So let's run that command. 3:22 It'll create the databases and set up all the tables for us. 3:25 Next, let's run the rails server and try the app out, bin/rails server, 3:30 go to our browser, and we'll visit localhost:3000, where the app is running. 3:36 We've shown an app like this previously, 3:45 it's just a simple guestbook app that can collect signatures from your visitors. 3:47 The list of signatures is totally blank since this is a new database, so 3:52 let's create a signature now. 3:55 Hello, PostgresQLI. 3:57 And there it is. 4:01 And there it is in our list. 4:03 Create another one. 4:05 And it looks like we're able to write signatures of that base and 4:13 read them back out. 4:15 We can also view the contents of the postgres database directly. 4:17 Just let me quit out of the server here. 4:22 And to connect to the database I'm gonna run the command bin/rails db. 4:25 This is a command built into rails that takes whatever database you 4:30 currently have set up and connects to it using that databases command line program. 4:33 So when I run the rails db commanded, 4:38 it'll connect me to the postgres terminal program. 4:40 From there I can run sequel commands like select * from signatures; 4:43 And we're able to see all the records that have been created via the web app. 4:51 Only when I exit the postgres terminal we can type backslash q, or 4:56 just hold CNTRL+D. 5:00 So that's it. 5:02 Our app seems to be working with the postgres database in the development 5:03 environment. 5:06 Next we're gonna show you how to set it up in production. 5:07
You need to sign up for Treehouse in order to download course files.Sign up