Use postgresql as a rails Database

Source: Internet
Author: User
Tags psql

Rails comes with a zero-configuration sqlite by default. This feature is OK for development, but cannot be written concurrently. It is not suitable for production. Let's start from scratch.

 

P.S.: deploy postgresql Based on ubuntu server

 

================ Install postgresql and start it ======================

First install postgresql in ubuntu

sudo apt-get install postgresql

Then start the postgresql Server

sudo /etc/init.d/postgresql-8.4 start

The default value of postgresql installed in 10.04 is 8.4. If it is 10.10, no version is required.

If you want to stop, change start to stop.


================ Configure postgresql ======================

Postgresql has only one user by default, that is, ipvs. Therefore, you can only switch to this user for logon.

 

sudo su postgres -c psql template1

Then change the postgres password.

 

ALTER USER postgres WITH PASSWORD 'newpassword';

Set the postgres system password.

 

sudo passwd postgres

Then create a user database, postgresql will create a user database for each user by default,

 

createdb    

Createdb is a database creation command, followed by a database name. If no parameter is added, it is the database name with the same name as the current user name by default,

After the database is created with psql daname to enter the postgresql console for a variety of operations, this part will not repeat, you can refer to the relevant information: http://www.cnblogs.com/klobohyz/archive/2012/01/04/2311834.html


==================== Configure a rails project =================================== =====

First of all, you can generate a rails project, or you already have a rails project. to change it to postgresql, You can first modify the Gemfile in the project file, and then add such a line.

 

gem 'pg'    

Run bundle install again

After the execution, the postgresql ruby component is installed. Now, configure config/database. yaml In the rails project.

 

 1 development:
2 adapter: postgresql
3 encoding: unicode
4 database: herkurails_development
5 username: root
6 password: 123qwe
7 pool: 5
8 timeout: 5000
9
10 # Warning: The database defined as "test" will be erased and
11 # re-generated from your development database when you run "rake".
12 # Do not set this db to the same as development or production.
13
14 test:
15 adapter: postgresql
16 encoding: unicode
17 database: herkurails_test
18 username: root
19 password: 123qwe
20 pool: 5
21 timeout: 5000
22
23
24 production:
25 adapter: postgresql
26 encoding: unicode
27 database: herkurails_production
28 username: root
29 pool: 5
30 password: root
31 timeout: 5000

Of course, the password and username must be changed to the postgresql user name. The above database parameter is the postgresql database name. You must go to postgresql to create these databases first, and do not expect rake automatically create and use the user specified in the configuration file to use createdb dbname to create the above database. After the database is created, return to the rails project and run rake db: migrate is used to modify the database operations of rails. Note that the current user who executes the rake command must be the user specified in the username. Otherwise, the execution will fail,


P.S.: if you do not want to use the default user, useCreateuser username to add a user, and then add a password for it,

 

ALTER USER username WITH PASSWORD 'new password'

Use this number to log on to ubuntu to perform the above operations. It is best to use an existing account of the system. If not, create a new system account with the same name.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.