---
title: "在ubuntu下跑 Postgresql 和 Rails3 基本"
author: "夜鸣猪"
date: "2012-09-04T21:06:14Z"
tags: []
categories: ["编程语言"]
slug: "ubuntu-postgresql-rails3"
draft: false
lastmod: "2012-09-04T21:06:14Z"
id: 1671464
url: "https://hlee.iteye.com/blog/1671464"
---

Rails 3 Installation

```java

sudo gem install rails
```

postgres as db installation<>br/


```java
$ sudo apt-get install postgresql
```


Rails 3 App with postgres as database


```java
$ rails new pg -d postgres
```

bundle installation
It will install dependency gems & postgres adapter for db connection

bundle install

Here by default 'postgres' database user gets created while installation but i recommend to create new db user with name same as of system user owning project directory permissions. 
User owning file permission can be found using

ls -l pg
drwxr-xr-x 7 sandip sandip 4096 2011-02-23 15:38 app
drwxr-xr-x 5 sandip sandip 4096 2011-02-23 18:14 config
-rw-r--r-- 1 sandip sandip  152 2011-02-23 15:38 config.ru
...
Default 'postgres' database user gets created while installation

## Snap of database.yml
development:
  adapter: postgresql
  encoding: unicode
  database: pg_development
  username: sandip
  pool: 5
Create new database user

pg $ sudo su postgres
pg $ createuser sandip
Shall the new role be a superuser? (y/n) y
pg $ exit
exit

Create a first development database:


```java
pg $ psql template1
```

Welcome to psql 8.4.6, the PostgreSQL interactive terminal.
...

```java

template1=# \l
```

        List of databases
   Name    |  Owner   | Encoding 
-----------+----------+----------
 postgres  | postgres | UTF8
 template0 | postgres | UTF8
 template1 | postgres | UTF8
(3 rows)


```java
template1=# CREATE DATABASE pg_development;
CREATE DATABASE
template1=# \l
```

            List of databases
       Name        |  Owner   | Encoding 
-------------------+----------+----------
 postgres          | postgres | UTF8
 pg_development | sandip   | UTF8
 template0         | postgres | UTF8
 template1         | postgres | UTF8
(4 rows)

```java

template1=# \q
```


Start rails server

pg $ rails s
Getting hands on postgres terminal
1. Login onto terminal

psql -U DB_USERNAME -d DB_NAME -W DB_PASSWORD
2. List databases

\l
3. Display tables

\dt
4. Exit from terminal

\q