Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Saturday, September 15, 2012

Replace PostgreSQL 9.1 with 9.2 in Ubuntu 12.04

Recently released PostgreSQL 9.2 comes with a heap of new features and improvements over 9.1.

These are the steps I took to remove 9.1 and install PostgreSQL 9.2 in Ubuntu 12.04.

Warning: This procedure is suitable for test and development environments only as it will wipe out all 9.1 databases


1. List currently installed postgres packages


sudo dpkg -l | grep postgres


2. Remove postgresql-9.1

*these packages were on my system

sudo apt-get --purge remove postgresql postgresql-9.1 postgresql-client-9.1 postgresql-client-common postgresql-common postgresql-doc postgresql-doc-9.1


3. Install postgresql-9.2 with official ppa


sudo add-apt-repository ppa:pitti/postgresql
sudo apt-get update
sudo apt-get install postgresql-9.2

If you cannot add the repository
sudo: add-apt-repository: command not found

Install this package

sudo apt-get install python-software-properties


4. Check postgresql is set to the default port 5432


sudo netstat -lp | grep postgresql


5. Reset to default port if necessary


sudo gedit /etc/postgresql/9.2/main/postgresql.conf

port = 5432 *change the port value in the conf file

sudo /etc/init.d/postgresql reload


6. Setup your project database / user and database.yml


PostgreSQL Setup for Rails Development (skip to step 2)


Sunday, August 12, 2012

Could not find a JavaScript runtime

Devs new to Linux will hit a 'Could not find a JavaScript runtime' error when trying to start a rails server.

The reason for this is Linux, by default, does not have an interface to a JavaScript engine.

Use one of these methods to install the interface required:

  1. Add The Ruby Racer gem to your Gemfile assets group:

    group :assets do
      gem 'therubyracer'
    end

    * Its a bit of a memory hog (Heroku discourage it) and you need to add it to every project you spin up on that machine.


  2. Install the Node.js package to your machine:

    sudo apt-get install nodejs

    * Distro specific installation: Installing Node.js by Package Manager


Monday, June 4, 2012

PostgreSQL setup for Rails development in Ubuntu 12.04

Rails comes with SQLite and it works out of the box with no configuration. That's great! but... its not suitable for a production environment so we need another database if your app is to see the light of day.

You could use SQLite in your development environment and switch it up for production, however, it makes far more sense to use the same database in development and production.

My personal preference is to use PostrgreSQL, its open source and gaining traction in the Rails world due to stability and rather nifty features. Heroku also uses PostgreSQL so its a good option if you plan on using their services.

See the excellent PostgreSQL Documentation for more information.

1. Install postgresql and admin tools through the package manager


sudo apt-get install postgresql libpq-dev phppgadmin pgadmin3

Change the postgresql configuration to solve this error
FATAL: Peer authentication failed for user

sudo nano /etc/postgresql/9.1/main/pg_hba.conf

Replace local peer with local md5 in the file. Then reload postgresql to apply the change.

sudo /etc/init.d/postgresql reload


2. Setup your databases from the command line


a) Login to postgresql prompt as the postgres user

sudo su postgres -c psql

b) Create a postgresql user for your project

create user username with password 'password';

TIP: If you are running tests, then to ease permission issues setup your postgres user with the same name and password as your Ubuntu user and make him a postgres superuser ( \du lists users with permissions).

alter user username superuser;


c) Create the development and test databases

create database projectname_development;
create database projectname_test;

d) Give permissions to the user on the databases

grant all privileges on database projectname_development to username;
grant all privileges on database projectname_test to username;

e) Finish your postgresql session

\q

Other useful commands in a postgresql session


Update password

alter user username with password ‘new password’;

Destroy database

drop database projectname;

Connect to specific database

\c databasename

Common MySQL commands with postgresql shortcut

MySQL Postresql
SHOW DATABASES \l
SHOW TABLES \d
SHOW COLUMNS \d table



3. Place a database configuration file in your rails project


Save the code below as app/config/database.yml and rake db:migrate your project

development:
  adapter: postgresql
  encoding: unicode
  database: projectname_development
  pool: 5
  username: username
  password: password

test:
  adapter: postgresql
  encoding: unicode
  database: projectname_test
  pool: 5
  username: username
  password: password




4. Misc Errors encountered


If you are importing data (or using a seed file) then you may run across this error.
PG::Error: ERROR: duplicate key value violates unique constraint "table_name_pkey"

Your import didn’t use the sequence so we need to reset it to be in sync again.

Note when replacing table_name use a plural.

sudo su postgres -c psql
\c database_name
select setval('table_name_id_seq', (select max(id) + 1 from table_name));