Setup Postgres for Ruby on rails on Linux Ubuntu

Setup Postgres for Ruby on rails on Linux Ubuntu

Before we install postgres, we should quick perform a quick update of the apt-get repository :

[code language="ruby"]
	apt-get update
[/code]

Once apt-get has updated go ahead and download Postgres and its helpful accompanying dependencies :

[code language="ruby"]
	sudo apt-get install postgresql postgresql-contrib
[/code]

 

During the installation, PostgreSQL will create a default user to operate under. We will be using this user for the initial steps. Log in with the following command :

[code language="ruby"]
	sudo su - postgres
[/code]

 

We start the prompt interface with the following command :

[code language="ruby"]
	psql
[/code]

 

To get a list of roles, type this :

[code language="ruby"]
	du
[/code]

[code language="ruby"]
	List of roles Role name | Attributes | Member of postgres | Superuser, Create role, Create DB, Replication | {}
[/code]

 

Get out of the PostgreSQL command prompt for a moment by typing :

[code language="ruby"]
	q
[/code]

 

Create a role called “user_test” with the following command :

[code language="ruby"]
	createuser user_test
[/code]

 

Shall the new role be a superuser? (y/n) n Shall the new role be allowed to create databases? (y/n) n Shall the new role be allowed to create more new roles? (y/n) n [/code]

 

You will be asked a series of questions that will define some initial permissions for the new role. If you answer “n” for no to all of these prompts, you will create a user similar to the previous user we created.

We can log back into the Postgres prompt and issue the “du” command again to see the differences between the two new roles :

[code language="ruby"]
	psql du
[/code]

[code language="ruby"]
	List of roles Role name | Attributes | Member of postgres | Superuser, Create role, Create DB, Replication | {} user_test | | {}
[/code]

 

For setup permission, we can change “user_test” role by issuing this command :

[code language="ruby"]
	ALTER ROLE user_test WITH CREATEDB;
[/code]

 

Let see new permission for user_test

[code language="ruby"]
	du
[/code]

[code language="ruby"]
	List of roles Role name | Attributes | Member of postgres | Superuser, Create role, Create DB, Replication | {} user_test | Create DB | {}
[/code]

 

Give the “user_test” a password with the following command :

[code language="ruby"]
	password user_test
[/code]

 

You will be prompted to enter and confirm a password. Now, exit the PostgreSQL interface and exit back to your normal user.

[code language="ruby"]
	q exit
[/code]

 

Let’s install phppgadmin, so we can easly to views and manage database. During its installation, phpPgAdmin will also install the required php and apache packages.

[code language="ruby"]
	sudo apt-get install postgresql postgresql-contrib phppgadmin
[/code]

 

Now you can access phppgadmin with http://localhost/phppgadmin Before you put “pg” on gemfile for your rails apps, make sure you run :

[code language="ruby"]
	sudo apt-get install libpq-devhttp://mrfrosti.com/wp-content/uploads/2011/06/PostgreSQL-9.gif
[/code]

 

And that’s it happy coding with Ruby on rails with Postgres Database!!

%d bloggers like this: