PostgreSQL is a mature and time-proven database. Also it is fast, secure and stable. And after all it’s free. So why not to use it in your next Rails project instead of default SQLite? The main drawback is that installing and setting up this database is a bit tricky. I’ve read many tutorials on the web which propose too many movements. But there is much easier way to accomplish this task. Let’s see how we can do this on Ubuntu 12.04.
First we need to install Postgres server. The easiest way to do this on Ubuntu is installing from package manager.
If everything goes well, we’ll have a fully configured and running database server.
To create new user and database we should enter database console as default user
If above does not work, try this:
Now crate user and database.
1 2 3
1 2 3
On development machine you should also lower security settings to simplify yor life a bit. Do not do this in production! Cahange your
/etc/postgresql/9.1/main/pg_hba.conf to this.
1 2 3
Then reload your database server to apply changes.
Rails application configuration
To use newly created Postgres database we should replace
config/database.yml content with this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
gem 'sqlite3' with
gem 'pg' in your
Gemfile and run
Then initialize your databases.
Deploy your application to your server. SSH to your server and set environment variable with database password inside your user’s
Then initialize your production database.
After restart your Rails application should use newly created Postgres database.
- Postgres 9.1 on Ubundu by default permits only local connections. See
/etc/postgresql/9.1/main/pg_hba.conffor proof. So we do not need to worry about external hacking of our DB.
- Unicode equals UTF8 in Postgres.