Installing Postgresql on Ubuntu for Ruby on Rails

Installing PostgreSQL on Ubuntu for Ruby on Rails

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

Getting PostgreSQL set up on Ubuntu bash running on Windows

Found the solution to these problems.

Issue #1 - 'pg' gem not installing
Execute the following in bash:

  1. sudo apt-get install libpq-dev
  2. gem install pg
  3. bundle install

Issue #2 - postgresql cannot start without crashing

Uninstall all versions on PSQL on computer and reinstall one. The failed installation and the second version were sharing their data folder and caused errors with the installation I was using.

PgAdmin on Windows 10 with Postgres when installed via Bash on Ubuntu on Windows

Here's what I did to connect Postgres DB installed in WSL Ubuntu from Windows pgAdmin.

  1. Launch Ubuntu in Windows.
  2. Start postgres in Ubuntu terminal: sudo service postgresql start
  3. Download the latest pgAdmin and install in Windows.
  4. Launch pgAdmin, a new tab in browser opens; click on Add New Server link.
  5. In the popup Create - Server window in the browser:

    1. General tab: I set Name to localhost
    2. Connection tab: I set Host name/address to localhost, set Password to postgres, which is the default, click on Save password?
    3. I save the setting, leaving the rest of the fields as is
  6. That's it, I can see the DB created in Postgres immediately.

Browser screenshot on adding a server

tips for installing postgres for a rails project on a linux box

This article from the Rails wiki should contain the information you are looking for.

I suggest you to install the pg GEM:

gem install pg

Also, Slicehost published a really interesting collection of article about using and configuring PostgreSQL on Ubuntu: http://articles.slicehost.com/postgresql



Related Topics



Leave a reply



Submit