macos ruby
When using the Ruby on Rails web framework, your application is set up by default to use SQLite as a database. SQLite is a lightweight, portable, and user-friendly relational database that performs especially well in low-memory environments, and will work well in many cases. However, for highly complex applications that need more reliable data integrity and programmatic extensibility, a PostgreSQL database will be a more robust and flexible choice. In order to configure your Ruby on Rails setup to use PostgreSQL, you will need to perform a few additional steps to get it up and running.
使用Ruby on Rails Web框架时,默认情况下会将您的应用程序设置为使用SQLite作为数据库。 SQLite是一个轻量级,可移植且用户友好的关系数据库,在低内存环境中表现特别出色,并且在许多情况下都能很好地工作。 但是,对于需要更可靠的数据完整性和程序可扩展性的高度复杂的应用程序, PostgreSQL数据库将是更健壮和灵活的选择。 为了将Ruby on Rails设置配置为使用PostgreSQL,您将需要执行一些其他步骤来启动和运行它。
In this tutorial, you will set up a Ruby on Rails development environment connected to a PostgreSQL database on a local macOS machine. You will install and configure PostgreSQL, and then test your setup by creating a Rails application that uses PostgreSQL as its database server.
在本教程中,您将设置一个Ruby on Rails开发环境,该环境连接到本地macOS计算机上的PostgreSQL数据库。 您将安装和配置PostgreSQL,然后通过创建一个将PostgreSQL用作数据库服务器的Rails应用程序来测试设置。
This tutorial requires the following:
本教程要求以下内容:
One computer or virtual machine with macOS installed, with administrative access to that machine and an internet connection. This tutorial has been tested on macOS 10.14 Mojave.
一台装有macOS的计算机或虚拟机,可以对该计算机进行管理访问并具有Internet连接。 本教程已在macOS 10.14 Mojave上进行了测试。
A Ruby on Rails development environment installed on your macOS machine. To set this up, follow our guide on How To Install Ruby on Rails with rbenv on macOS. This tutorial will use version 2.6.3 of Ruby and 5.2.3 of Rails; for information on the latest versions, check out the official sites for Ruby and Rails.
在macOS机器上安装了Ruby on Rails开发环境。 要进行设置,请遵循有关如何在macOS上使用rbenv安装Ruby on Rails的指南 。 本教程将使用2.6.3版的Ruby和5.2.3版的Rails。 有关最新版本的信息,请访问Ruby和Rails的官方网站。
In order to configure Ruby on Rails to create your web application with PostgreSQL as a database, you will first install the database onto your machine. Although there are many ways to install PostgreSQL on macOS, this tutorial will use the package manager Homebrew.
为了配置Ruby on Rails以PostgreSQL作为数据库来创建Web应用程序,您首先将数据库安装到您的机器上。 尽管有很多方法可以在macOS上安装PostgreSQL ,但本教程将使用软件包管理器Homebrew 。
There are multiple Homebrew packages to install different versions of PostgreSQL. To install the latest version, run the following command:
有多个Homebrew软件包可以安装不同版本的PostgreSQL。 要安装最新版本,请运行以下命令:
If you would like to download a specific version of PostgreSQL, replace postgresql
in the previous command with your desired package. You can find the available packages at the Homebrew website.
如果要下载特定版本的PostgreSQL,请使用所需的软件包替换上一个命令中的postgresql
。 您可以在Homebrew网站上找到可用的软件包。
Next, include the PostgreSQL binary in your PATH
variable in order to access the PostgreSQL command line tools, making sure to replace the 10
with the version number you are using:
接下来,在您的PATH
变量中包含PostgreSQL二进制文件,以便访问PostgreSQL命令行工具,并确保将10
替换为您使用的版本号:
echo 'export PATH="/usr/local/opt/postgresql@10/bin:$PATH"' >> ~/.bash_profile
echo'export PATH =“ / usr / local / opt / postgresql @ 10 / bin:$ PATH”'>>〜/ .bash_profile
Then, apply the changes you made to your ~/.bash_profile
file to your current shell session:
然后,将对~/.bash_profile
文件所做的更改应用于当前的shell会话:
To start the service and enable it to start at login, run the following:
要启动该服务并使它能够在登录时启动,请运行以下命令:
brew services start postgresql@10
brew服务启动postgresql @ 10
Check to make sure the installation was successful:
检查以确保安装成功:
You will get the following output:
您将获得以下输出:
Output
postgres (PostgreSQL) 10.9
Once PostgreSQL is installed, the next step is to create a role that your Rails application will use later to create your database.
一旦安装了PostgreSQL,下一步就是创建一个角色,Rails应用程序以后将使用该角色来创建数据库。
In PostgreSQL, roles can be used to organize permissions and authorization. When starting PostgreSQL with Homebrew, you will automatically have a superuser role created with your macOS username. In order to keep these superuser privileges separate from the database instance you use for your Rails application, in this step you will create a new role with less access.
在PostgreSQL中, 角色可用于组织权限和授权。 使用Homebrew启动PostgreSQL时,您将自动使用macOS用户名创建一个超级用户角色。 为了使这些超级用户特权与用于Rails应用程序的数据库实例分开,在此步骤中,您将创建一个具有较少访问权限的新角色。
To create a new role, run the following command, replacing appname
with whatever name you’d like to give the role:
要创建新角色,请运行以下命令,将appname
替换为您想要赋予该角色的任何名称:
createuser -P -d appname
CREATEUSER -p -D APPNAME
In this command, you used createuser
to create a role named appname
. The -d
flag gave the role the permission to create new databases.
在此命令中,您使用createuser
创建了一个名为appname
的角色。 -d
标志授予该角色创建新数据库的权限。
You also specified the -P
flag, which means you will be prompted to enter a password for your new role. Enter your desired password, making sure to record it so that you can use it in a configuration file in a future step.
您还指定了-P
标志,这意味着将提示您输入新角色的密码。 输入所需的密码,并确保将其记录下来,以便以后在配置文件中使用它。
If you did not use the -P
flag and want to set a password for the role after you create it, enter the PostgreSQL console with the following command:
如果您没有使用-P
标志,并且想要在创建角色后为该角色设置密码,请使用以下命令进入PostgreSQL控制台:
You will receive the following output, along with the prompt for the PostgreSQL console:
您将收到以下输出以及PostgreSQL控制台的提示:
Output
psql (10.9)
Type "help" for help.
postgres=#
The PostgreSQL console is indicated by the postgres=#
prompt. At the PostgreSQL prompt, enter this command to set the password for the new database role, replacing the highlighted name with the one you created:
PostgreSQL控制台由postgres=#
提示符指示。 在PostgreSQL提示符下,输入以下命令来设置新数据库角色的密码,将突出显示的名称替换为您创建的名称:
\password appname
\ password appname
PostgreSQL will prompt you for a password. Enter your desired password at the prompt, then confirm it.
PostgreSQL将提示您输入密码。 在提示符下输入所需的密码,然后确认。
Now, exit the PostgreSQL console by entering this command:
现在,通过输入以下命令退出PostgreSQL控制台:
Your usual prompt will now reappear.
现在,通常的提示将重新出现。
In this step, you created a new PostgreSQL role without superuser privileges for your application. Now you are ready to create a new Rails app that uses this role to create a database.
在此步骤中,您为应用程序创建了一个没有超级用户特权的新PostgreSQL角色。 现在您可以创建一个新的Rails应用程序,该应用程序使用此角色来创建数据库。
With your role configured for PostgreSQL, you can now create a new Rails application that is set up to use PostgreSQL as a database.
通过为PostgreSQL配置角色,您现在可以创建一个新的Rails应用程序,该应用程序设置为将PostgreSQL用作数据库。
First, navigate to your home directory:
首先,导航到您的主目录:
Create a new Rails application in this directory, replacing appname
with whatever you would like to call your app:
在此目录中创建一个新的Rails应用程序,将appname
替换为您想调用的应用程序名称:
rails new appname -d=postgresql
轨新应用程序名称 -d = PostgreSQL
The -d=postgresql
option sets PostgreSQL as the database.
-d=postgresql
选项将PostgreSQL设置为数据库。
Once you’ve run this command, a new folder named appname
will appear in your home directory, containing all the elements of a basic Rails application.
运行此命令后,将在主目录中出现一个名为appname
的新文件夹,其中包含基本Rails应用程序的所有元素。
Next, move into the application’s directory:
接下来,进入应用程序的目录:
cd appname
CD APPNAME
Now that you have created a new Rails application and have moved into the root directory for your project, you can configure and create your PostgreSQL database from within your Rails app.
现在,您已经创建了一个新的Rails应用程序,并且已经移到项目的根目录中,您可以在Rails应用程序中配置和创建PostgreSQL数据库。
When creating the development
and test
databases for your application, Rails will use the PostgreSQL role that you created in Step 2. To make sure that Rails creates these databases, you will alter the database configuration file of your project. You will then create your databases.
在为应用程序创建development
和test
数据库时,Rails将使用您在步骤2中创建的PostgreSQL角色。为确保Rails创建了这些数据库,您将更改项目的数据库配置文件。 然后,您将创建数据库。
One of the configuration changes to make in your Rails application is to add the password for the PostgreSQL role you created in the last step. To keep sensitive information like passwords safe, it is a good idea to store this in an environment variable rather than to write it directly in your configuration file.
在Rails应用程序中要进行的配置更改之一是添加在上一步中创建的PostgreSQL角色的密码。 为了确保密码等敏感信息的安全,最好将其存储在环境变量中,而不是直接将其写入配置文件中。
To store your password in an environment variable at login, run the following command, replacing APPNAME
with the name of your app and PostgreSQL_Role_Password
with the password you created in the last step:
要将密码存储在登录时的环境变量中,请运行以下命令,将APPNAME
替换为应用程序的名称,并将PostgreSQL_Role_Password
替换为在上一步中创建的密码:
echo 'export APPNAME_DATABASE_PASSWORD="PostgreSQL_Role_Password"' >> ~/.bash_profile
回声'导出APPNAME _DATABASE_PASSWORD =“ PostgreSQL_Role_Password ”'>>〜/ .bash_profile
This command writes the export
command to your ~/.bash_profile
file so that the environment variable will be set at login.
此命令将export
命令写入您的~/.bash_profile
文件,以便在登录时设置环境变量。
To export the variable for your current session, use the source
command:
要导出当前会话的变量,请使用source
命令:
Now that you have stored your password in your environment, it’s time to alter the configuration file.
既然您已经将密码存储在您的环境中,那么现在该更改配置文件了。
Open your application’s database configuration file in your preferred text editor. This tutorial will use nano
:
在首选的文本编辑器中打开应用程序的数据库配置文件。 本教程将使用nano
:
Under the default
section, find the line that says pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
and add the following highlighted lines, filling in your credentials and the environment variable you created. It should look something like this:
在default
部分下,找到显示pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
的行pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
并添加以下突出显示的行,以填充您的凭据和创建的环境变量。 它看起来应该像这样:
...
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
username: appname
password: <%= ENV['APPNAME_DATABASE_PASSWORD'] %>
development:
<<: *default
database: appname_development
...
This will make the Rails application run the database with the correct role and password. Save and exit by pressing CTRL+X
, Y
, then ENTER
.
这将使Rails应用程序以正确的角色和密码运行数据库。 按CTRL+X
, Y
,然后按ENTER
保存并退出。
For more information on configuring databases in Rails, see the Rails documentation.
有关在Rails中配置数据库的更多信息,请参见Rails文档 。
Now that you have made changes to config/database.yml
, create your application’s databases by using the rails
command:
现在,您已经对config/database.yml
进行了更改,使用rails
命令创建应用程序的数据库:
Once Rails creates the database, you will receive the following output:
Rails创建数据库后,您将收到以下输出:
Output
Created database 'appname_development'
Created database 'appname_test'
As the output suggests, this command created a development
and test
database in your PostgreSQL server.
如输出所示,此命令在PostgreSQL服务器中创建了一个development
和test
数据库。
You now have a PostgreSQL database connected to your Rails app. To ensure that your application is working, the next step is to test your configuration.
现在,您已将PostgreSQL数据库连接到Rails应用程序。 为确保您的应用程序正常运行,下一步是测试您的配置。
To test that your application is able to use the PostgreSQL database, try to run your web application so that it will show up in a browser.
要测试您的应用程序是否可以使用PostgreSQL数据库,请尝试运行您的Web应用程序,使其在浏览器中显示。
First, you’ll use the built-in web server for Rails, Puma, to serve your application. This web server comes with Rails automatically and requires no additional setup. To serve your application, run the following command:
首先,您将使用Rails的内置Web服务器Puma来服务您的应用程序。 该Web服务器是Rails自动附带的,不需要其他设置。 要为您的应用程序提供服务,请运行以下命令:
--binding
binds your application to a specified IP. By default, this flag will bind Rails to 0.0.0.0
, but since this means that Rails will listen to all interfaces, it is more secure to use 127.0.0.1
to specify the localhost
. By default, the application listens on port 3000
.
--binding
将您的应用程序绑定到指定的IP。 默认情况下,此标志会将Rails绑定到0.0.0.0
,但是由于这意味着Rails将侦听所有接口,因此使用127.0.0.1
指定localhost
更加安全。 默认情况下,应用程序在端口3000
上侦听。
Once your Rails app is running, your command prompt will disappear, replaced by this output:
一旦您的Rails应用程序运行,您的命令提示符将消失,由以下输出代替:
Output
=> Booting Puma
=> Rails 5.2.3 application starting in development
=> Run `rails server -h` for more startup options
Puma starting in single mode...
* Version 3.12.1 (ruby 2.6.3-p62), codename: Llamas in Pajamas
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://127.0.0.1:3000
Use Ctrl-C to stop
To test if your application is running, open up a new terminal window on your server and use the curl
command to send a request to 127.0.0.1:3000
:
要测试您的应用程序是否正在运行,请在服务器上打开一个新的终端窗口,并使用curl
命令将请求发送到127.0.0.1:3000
:
You will receive a lot of output in HTML, ending in something like:
您会收到许多HTML输出,结尾为:
Output
...
<strong>Rails version:</strong> 5.2.3<br />
<strong>Ruby version:</strong> 2.6.3 (x86_64-darwin18)
</p>
</section>
</div>
</body>
</html>
You can also access your Rails application in a local web browser by visiting:
您还可以通过以下方式在本地Web浏览器中访问Rails应用程序:
http://127.0.0.1:3000
At this URL, you will find a Ruby on Rails welcome page:
在此URL上,您将找到Ruby on Rails欢迎页面:
This means that your application is properly configured and connected to the PostgreSQL database.
这意味着您的应用程序已正确配置并连接到PostgreSQL数据库。
In this tutorial, you created a Ruby on Rails web application that was configured to use PostgreSQL as a database on a local macOS machine. If you would like to learn more about the Ruby programming language, check out our How To Code in Ruby series.
在本教程中,您创建了一个Ruby on Rails Web应用程序,该应用程序配置为将PostgreSQL用作本地macOS计算机上的数据库。 如果您想了解有关Ruby编程语言的更多信息,请查看我们的《 如何在Ruby中编码》系列 。
For more information on choosing a database for your application, check out our tutorial on the differences between and use cases of SQLite, PostgreSQL, and MySQL. If you want to read more about how to use databases, see our An Introduction to Queries in PostgreSQL article, or explore DigitalOcean’s Managed Databases product.
有关为您的应用程序选择数据库的更多信息,请查看有关SQLite,PostgreSQL和MySQL之间的区别和用例的教程。 如果您想了解有关如何使用数据库的更多信息,请参见PostgreSQL中的查询简介一文,或浏览DigitalOcean的托管数据库产品 。
macos ruby