How To Use PostgreSQL with Your Ruby on Rails Application on Ubuntu 14.04
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
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):
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 host
, username
, 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:
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的更多相关文章
- ubuntu 14.04中安装 ruby on rails 环境
环境:在win7 上Vmware虚拟机环境中安装的ubuntu 14.04 1. bundle install 时,报json错误可以看出是在安装nokogiri时遇到了问题,此时执行 sudo ap ...
- ubuntu 14.04中安装 ruby on rails 环境(填坑版) 呕血推荐
环境:在win7 上Vmware虚拟机环境中安装的ubuntu 14.04 开发相关: ruby 2.2.0 rails 4.2.0 sublime text 3 本文说明:所有的命令均在$ 之后,若 ...
- ubuntu 14.04 源码编译postgresql
环境 ubuntu 14.04 桌面版 postgresql 源码下载链接,本教程是使用postgresql 9.3.4 进行编译的 http://www.postgresql.org/ftp/sou ...
- Ubuntu 14.04 Ruby 2.3.3 安装
在Ubuntu 14.04通过下载Ruby源码包进行安装. 第一步,更新apt-get sudo apt-get update 通过apt-get安装ruby依赖 sudo apt-get insta ...
- ubuntu 14.04安装postgresql最新版本
官网:https://www.postgresql.org/download/linux/ubuntu/ ----------------------------------------------- ...
- 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 ...
- 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 ...
- ubuntu 14.04安装 ruby on rails
安装完成ubuntu14.04以后 第一步: 1.sudo apt-get update 2.sudo apt-get upgrade 第二步: RVM 安装 $ gpg --keyserver ...
- 【Ruby on Rails 学习一】ubuntu14.04配置rvm与ruby
要安装ruby,首先要安装rvm,借助rvm安装ruby rvm 的全称是 Ruby Version Manager ,是一款由 Wayne E. Seguin 开发的一款命令行工具.rvm 能够让 ...
随机推荐
- Spark核心原理初探
一.运行架构概览 Spark架构是主从模型,分为两层,一层管理集群资源,另一层管理具体的作业,两层是解耦的.第一层可以使用yarn等实现. Master是管理者进程,Worker是被管理者进程,每个W ...
- C# GDI+简单绘图
一.使用Pen画笔 Pen的主要属性有: Color(颜色),DashCap(短划线终点形状),DashStyle(虚线样式),EndCap(线尾形状), StartCap(线头形状),Width(粗 ...
- 洛谷P2015 二叉苹果树(树状dp)
题目描述 有一棵苹果树,如果树枝有分叉,一定是分2叉(就是说没有只有1个儿子的结点) 这棵树共有N个结点(叶子点或者树枝分叉点),编号为1-N,树根编号一定是1. 我们用一根树枝两端连接的结点的编号来 ...
- 小程序 ios 10.1.x 白屏问题
遇到这个问题有点莫名其妙只有IOS10.1.x显示白屏 解决过程很坎坷,一开始以为是调试基础库的问题,改成最新的2.9.2版本. 用这个创建新的项目,把之前的项目内容放到新建项目内, 发现还是不行,后 ...
- 【数据库】一篇文章搞掂:SQL Server数据库
问题: 1.同一段代码,在存储过程中运行比普通SQL执行速度慢几十倍 原理: 在SQL Server中有一个叫做 “Parameter sniffing”参数嗅探的特性.SQL Server在存储过程 ...
- 关于最近练习PYTHON代码的一点心得
做测试以来,一直想学习代码,以前也断断续续的学习过,不过都是练习一些基础语法,学习的是菜鸟教程,但是效果不大. 最近在练习CODEWAR里做练习题,慢慢强化自己对一些基本语法的理解,熟悉基本的内置函数 ...
- (转)SQL Server 2012 手动安装帮助文档+排错
逆天SQL Server 2012装的不要不要的,最后发现...竟然没帮助文档...汗啊!原来它跟vs一样要自己装帮助文档...好吧,官网一下载,妹的...报错...然后就让我们还原这个安装过程以及逆 ...
- 破解Xshell6强制升级
一.背景今天打开xshell时,弹出提示,“要继续使用此程序,您必须应用最新的更新或使用新版本”(如下图) 这是让我强制升级啊,点了确定按钮却提示我已经是最新版了 反正点了半天xshell也没打开.后 ...
- Emacs 25.1 error solved: url-http-create-request: Multibyte text in HTTP request
Emacs 25.1 error solved: url-http-create-request: Multibyte text in HTTP request */--> code {colo ...
- [FW]CLONE_NEWUSER trickery: CVE-2013-1858
CLONE_NEWUSER trickery: CVE-2013-1858 Recent kernels (3.8+ something) introduced a feature calledu ...