How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04

链接来自于:https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-ruby-on-rails-application-on-ubuntu-14-04

测试成功,可以实现rails database based on postgresql

Introduction

Ruby on Rails uses sqlite3 as its default database, which works great in many cases, but may not be sufficient for your application. If your application requires the scalability, centralization, and control (or any other feature) that is provided by a client/server SQL database, such as PostgreSQL or MySQL, you will need to perform a few additional steps to get it up and running.

This tutorial will show you how to set up a development Ruby on Rails environment that will allow your applications to use a PostgreSQL database, on an Ubuntu 14.04 server. First, we will cover how to install and configure PostgreSQL. Then we'll show you how to create a rails application that uses PostgreSQL as its database server.

 

Prerequisites

This tutorial requires that have a working Ruby on Rails development environment. If you do not already have that, you may follow the tutorial in this link: How To Install Ruby on Rails with rbenv on Ubuntu 14.04.

You will also need to have access to a superuser, or sudo, account, so you can install the PostgreSQL database software.

Once you're ready, let's install PostgreSQL.

 

Install PostgreSQL

If you don't already have PostgreSQL installed, let's do that now.

First, update apt-get:

  • sudo apt-get update

Then install PostgreSQL and its development libraries:

  • sudo apt-get install postgresql postgresql-contrib libpq-dev

PostgreSQL is now installed but you should create a new database user, that your Rails application will use.

 

Create Database User

Create a PostgreSQL superuser user with this command (substitute the highlighted word with your own username):

  • sudo -u postgres createuser -s pguser

If you want to set a password for the database user, enter the PostgreSQL console with this command:

  • sudo -u postgres psql

The PostgreSQL console is indicated by the postgres=# prompt. At the PostgreSQL prompt, enter this command to set the password for the database user that you created:

  • \password pguser

Enter your desired password at the prompt, and confirm it.

Now you may exit the PostgreSQL console by entering this command:

  • \q

Let's create a Rails application now.

 

Create New Rails Application

Create a new Rails application in your home directory. Use the -d postgresql option to set PostgreSQL as the database, and be sure to substitute the highlighted word with your application name:

  • cd ~
  • rails new appname -d postgresql

Then move into the application's directory:

  • cd appname

The next step is to configure the application's database connection.

Configure Database Connection

The PostgreSQL user that you created will be used to create your application's test and development databases. We need to configure the proper database settings for your application.

Open your application's database configuration file in your favorite text editor. We'll use vi:

  • vi config/database.yml

Under the default section, find the line that says "pool: 5" and add the following lines under it. It should look something like this (replace the highlighted parts with your PostgreSQL user and password):

config/database.yml excerpt
  host: localhost
username: pguser
password: pguser_password

Save and exit.

Create Application Databases

Create your application's development and test databases by using this rake command:

  • rake db:create

This will create two databases in your PostgreSQL server. For example, if your application's name is "appname", it will create databases called "appname_development" and "appname_test".

If you get an error at this point, revisit the previous subsection (Configure Database Connection) to be sure that the hostusername, and password in database.yml are correct. After ensuring that the database information is correct, try creating the application databases again.

 

Test Configuration

The easiest way to test that your application is able to use the PostgreSQL database is to try to run it.

For example, to run the development environment (the default), use this command:

  • rails server

This will start your Rails application on your localhost on port 3000.

If your Rails application is on a remote server, and you want to access it through a web browser, an easy way is to bind it to the public IP address of your server. First, look up the public IP address of your server, then use it with the rails server command like this (substitute it for the highlighted part):

  • rails server --binding=server_public_IP

Now you should be able to access your Rails application in a web browser via the server's public IP address on port 3000:

Visit in a web browser:
http://server_public_IP:3000

If you see the "Welcome aboard" Ruby on Rails page, your application is properly configured, and connected to the PostgreSQL database.

How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04的更多相关文章

  1. ubuntu 14.04中安装 ruby on rails 环境

    环境:在win7 上Vmware虚拟机环境中安装的ubuntu 14.04 1. bundle install 时,报json错误可以看出是在安装nokogiri时遇到了问题,此时执行 sudo ap ...

  2. ubuntu 14.04中安装 ruby on rails 环境(填坑版) 呕血推荐

    环境:在win7 上Vmware虚拟机环境中安装的ubuntu 14.04 开发相关: ruby 2.2.0 rails 4.2.0 sublime text 3 本文说明:所有的命令均在$ 之后,若 ...

  3. ubuntu 14.04 源码编译postgresql

    环境 ubuntu 14.04 桌面版 postgresql 源码下载链接,本教程是使用postgresql 9.3.4 进行编译的 http://www.postgresql.org/ftp/sou ...

  4. Ubuntu 14.04 Ruby 2.3.3 安装

    在Ubuntu 14.04通过下载Ruby源码包进行安装. 第一步,更新apt-get sudo apt-get update 通过apt-get安装ruby依赖 sudo apt-get insta ...

  5. ubuntu 14.04安装postgresql最新版本

    官网:https://www.postgresql.org/download/linux/ubuntu/ ----------------------------------------------- ...

  6. Installing Ruby 1.9.3 on Ubuntu 12.04 Precise Pengolin (without RVM)

    02 MAY, 2012 The new Ubuntu release has just rolled around and with it a slew of new packages. Perso ...

  7. Ruby on Rails Session 1: How to Build a Ruby on Rails on the Ubuntu.

    About Ruby on Rails Ruby on Rails is an application stack that provides developers with a framework ...

  8. ubuntu 14.04安装 ruby on rails

    安装完成ubuntu14.04以后 第一步: 1.sudo apt-get update 2.sudo apt-get upgrade 第二步:   RVM 安装 $ gpg --keyserver ...

  9. 【Ruby on Rails 学习一】ubuntu14.04配置rvm与ruby

    要安装ruby,首先要安装rvm,借助rvm安装ruby rvm 的全称是 Ruby Version Manager ,是一款由 Wayne E. Seguin  开发的一款命令行工具.rvm 能够让 ...

随机推荐

  1. javaweb判断当前请求是否为移动设备访问的方法

    由于移动端和pc端还是稍微有些区别的,我觉得最好是在一个地儿统一判断,而且不要改动原先的代码,这样可以从一定程度上减少bug的数量.我的想法是首先应该判断当前请求是否为移动端,然后设一个标识到sess ...

  2. 阿里云重磅发布RDS for SQL Server AlwaysOn集群版

    2018年双十一刚过,阿里云数据库发布RDS for SQL Server AlwaysOn集群版,这是业界除微软云SQL Database外,首家云计算公司基于SQL Server最新AlwaysO ...

  3. Service系统服务(五):PXE基础装机环境、配置并验证DHCP服务、配置PXE引导、验证PXE网络装机、PXE+kickstart自动装机

    一.PXE基础装机环境 目标: 本例要求为后续的PXE服务器构建提供RHEL7软件仓库,完成下列任务: 1> 在CentOS真机部署Web目录/var/www/html/rh7dvd   2&g ...

  4. 【HDOJ6581】Vacation(模拟)

    题意:有标号从0到n的n+1辆车,每辆车离终点的距离为s[i],车长为l[i],速度为v[i],规定不能超车,问何时所有车都能过终点线 n<=1e5,保证 思路: #include<bit ...

  5. 75 OpenCV编译、图像处理等

    0 引言 记录图像处理的一些经验和使用OpenCV 等库的注意事项. 1 opencv中的坐标系 一图以蔽之~ 2 opencv 3.4.0 + opencv_contrib + qt编译 主要参考了 ...

  6. linux日常---1、linux下安装、查看、卸载包常用命令

    linux日常---1.linux下安装.查看.卸载包常用命令 一.总结 一句话总结: 对比学习 1.linux如何查看系统中安装的程序? rpm -qa   # 查看所有安装的软件包 2.linux ...

  7. cs224d 作业 problem set2 (三) 用RNNLM模型实现Language Model,来预测下一个单词的出现

      今天将的还是cs224d 的problem set2 的第三部分习题, 原来国外大学的系统难度真的如此之大,相比之下还是默默地再天朝继续搬砖吧 下面讲述一下RNN语言建模的数学公式: 给出一串连续 ...

  8. 10 面向对象(package关键字的概述及作用)

    10.01_面向对象(package关键字的概述及作用) A:为什么要有包 *开发时有很多类,如果放在一个文件夹中不方便管理,而且容易重复 将字节码(.class)进行分类存放 包其实就是文件夹 B: ...

  9. 力扣算法题—147Insertion_Sort_List

    Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...

  10. Django框架(十八)—— auth框架:用户登录、注册、认证

    目录 auth模块 一.什么是author模块 二.auth模块的使用 1.创建超级用户(create_superuser()) 2.验证用户(authenticate()) 3.登录用户(login ...