I currently have Ruby on Rails installed via RVM in Ubuntu 12.04. The default database is set up in SQLite3, but I'd like to switch to PostgreSQL for the purposes of pushing to Heroku. How can I accomplish this?
3 Answers
Here are the steps I've followed:
Install PostgreSQL and development package
$ sudo apt-get install postgresql
$ sudo apt-get install libpq-dev
Set up a user that is the same as my Ubuntu log-in
$ sudo su postgres -c psql
postgres=# CREATE ROLE <username> SUPERUSER LOGIN;
postgres=# \q
Modify Gemfile
# Remove gem 'sqlite3'
gem 'pg'
Modify database.yml
in app directory
development:
adapter: postgresql
encoding: unicode
database: appname_development
pool: 5
timeout: 5000
username: <username>
password:
test:
adapter: postgresql
encoding: unicode
database: appname_test
pool: 5
timeout: 5000
username: <username>
password:
Run bundle install
$ bundle install
Create databases and migrations
$ rake db:create:all
$ rake db:migrate
Here are the sources I used to help:
http://mrfrosti.com/2011/11/postgresql-for-ruby-on-rails-on-ubuntu/
http://railscasts.com/episodes/342-migrating-to-postgresql
https://devcenter.heroku.com/articles/local-postgresql
-
14If you want to add a password use this command after running `CREATE ROLE`: `ALTER ROLE
WITH PASSWORD ' – Hengjie Jan 24 '13 at 05:33';` then obviously add your password into `database.yml` -
9Alternatively, if you're just creating the role for the first time and want to enter in a password as well: `CREATE ROLE
SUPERUSER LOGIN PASSWORD ' – Hengjie Jan 24 '13 at 05:40';` -
If you set the user as identical with your unix username, is this secure even if there is no password? Based on, e.g., the mrfrosti.com link above, and also Heroku's Postgress.app, it seems like that's often the suggested route. – Dav Clark Feb 05 '13 at 17:02
-
here's instructions for installing 9.2 on ubuntu http://askubuntu.com/questions/186610/how-do-i-upgrade-to-postgres-9-2/257827#257827 – Danny Feb 18 '13 at 17:30
-
1FATAL: Peer authentication failed for user "awesome" – jmontross May 09 '13 at 04:38
-
I was switching from using tiny_tds. If you are swapping from one db to another you might want to do `bundle update` to your app before running `rake db:create:all` – daveomcd Jun 30 '13 at 15:30
-
1Worked for me. I created my psql user with following command: `CREATE ROLE Alex SUPERUSER LOGIN;`. In the database.yml I also set my username as `Alex` and got the same error as jmontross. Then I changed it to `alex` and everything worked. I also had to run `rake db:migrate RAILS_ENV=test`. Also note - this method migrates the database from sqlite3 to postgres, but does not migrate the actual data in the database. To migrate it as well, refer to the railscast mentioned in the answer. – Alexander Popov Aug 21 '13 at 19:46
-
This is great info but do you have any idea how I would configure Postgres for production? I looked at all the links you posted and none of them mention this. They just say "don't do this in production" and talk about how to configure for test and development. I posted a [question](http://stackoverflow.com/questions/23142074/configuring-rails-using-posgres-in-production) about this on here but there are no answers so far. – Adam Apr 19 '14 at 02:06
-
@Adam - If you're deploying to Heroku, they will automatically set your production config. You shouldn't need to specify production values in your `database.yml` file. For more info: https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-ruby – Nick Apr 23 '14 at 13:15
-
Thanks Nick. Someone else told me that as well. Heroku is great, but I was wanting to set up my own server from scratch, just to learn how it's done. – Adam Apr 24 '14 at 01:15
-
12 years later, the only thing that's a bit old from this answer is the postgresql version. Using the command `sudo apt-get install postgresql` will install the latest available version. – alf May 18 '14 at 23:30
For all Ubuntu 13.10
users that open this thread follow the steps below to install postresql
:
sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-common -t saucy
sudo apt-get install postgresql-9.2 libpq-dev
since there isn't an official Postgres repository for Ubuntu 13.10
.
Then create the user as Nick
explain (you can specify a password too):
sudo su postgres -c psql
postgres=# CREATE ROLE gotqn SUPERUSER LOGIN;
postgres=# \password gotqn
postgres=# \q
Note: Replace the gotqn
above with whoami
result:
The easiest way to create your rails application is to specify you are using postgresql
as follows:
rails new Demo -d postgresql
The code above will automatically add the pg
gem in your GemFile
and create appropriate database.yml
file:
development:
adapter: postgresql
encoding: unicode
database: Demo_development
pool: 5
username: gotqn
password: mypass
Note: You need to change the username and to specify the correct password if you have set such.
Then run rake db:create
and start the rails server.

- 42,737
- 46
- 157
- 243
sudo sh -c "echo 'deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main' > /etc/apt/sources.list.d/pgdg.list"
wget --quiet -O - http://apt.postgresql.org/pub/repos/apt/ACCC4CF8.asc | sudo apt-key add -
sudo apt-get update
sudo apt-get install postgresql-common
sudo apt-get install postgresql-9.3 libpq-dev

- 169
- 2
- 5