rails中.bundle_如何在Ubuntu 18.04上的Ruby on Rails应用程序中使用PostgreSQL

rails中.bundle

介绍 (Introduction)

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 an Ubuntu 18.04 server. 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开发环境,该环境连接到Ubuntu 18.04服务器上的PostgreSQL数据库。 您将安装和配置PostgreSQL,然后通过创建一个将PostgreSQL用作数据库服务器的Rails应用程序来测试设置。

先决条件 (Prerequisites)

This tutorial requires the following:

本教程要求以下内容:

第1步-安装PostgreSQL (Step 1 – Installing PostgreSQL)

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 server.

为了将Ruby on Rails配置为使用PostgreSQL作为数据库创建Web应用程序,您首先将数据库安装到服务器上。

Using sudo privileges, update your APT package index to make sure that your repositories are up to date:

使用sudo特权,更新您的APT软件包索引,以确保您的存储库是最新的:

  • sudo apt update

    sudo apt更新

Next, install PostgreSQL and its development libraries:

接下来,安装PostgreSQL及其开发库:

  • sudo apt install postgresql postgresql-contrib libpq-dev

    sudo apt安装postgresql postgresql-contrib libpq-dev

In the previous command, the postgresql package holds the main PostgreSQL program, while postgresql-contrib adds several PostgreSQL features that extend its capabilities. libpq-dev is a PostgreSQL library that allows clients to send queries and receive responses from the back-end server, which will allow your application to communicate with its database.

在上一个命令中, postgresql软件包包含主要的PostgreSQL程序,而postgresql-contrib添加了一些PostgreSQL功能以扩展其功能。 libpq-dev是一个PostgreSQL库,它允许客户端发送查询和接收来自后端服务器的响应,这将使您的应用程序与其数据库进行通信。

Once PostgreSQL and its dependencies are installed, the next step is to create a role that your Rails application will use later to create your database.

一旦安装了PostgreSQL及其依赖项,下一步就是创建一个角色,Rails应用程序以后将使用该角色来创建数据库。

步骤2 –创建新的数据库角色 (Step 2 – Creating a New Database Role)

In PostgreSQL, roles can be used in the same way as users in Linux to organize permissions and authorization. This step will show you how to create a new super user role for your Linux username that will allow you to operate within the PostgreSQL system to create and configure databases.

在PostgreSQL中,可以以与Linux中的用户相同的方式来使用角色来组织权限和授权。 此步骤将向您展示如何为Linux用户名创建新的超级用户角色,该角色将使您能够在PostgreSQL系统中操作以创建和配置数据库。

To create a PostgreSQL super user role, use the following command, substituting the highlighted word with your Ubuntu 18.04 username:

要创建PostgreSQL超级用户角色,请使用以下命令,将突出显示的单词替换为您的Ubuntu 18.04用户名:

  • sudo -u postgres createuser -s sammy -P

    须藤-u postgres createuser -s sammy -P

Since you specified the -P flag, 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标志,因此将提示您输入新角色的密码。 输入所需的密码,并确保将其记录下来,以便以后在配置文件中使用它。

In this command, you used createuser to create a role named sammy. The -s gave this user super user privileges, and sudo -u allowed you to run the command from the postgres account that is automatically created upon installing PostgreSQL.

在此命令中,您使用createuser创建了一个名为sammy的角色。 -s为该用户赋予了超级用户特权,并且sudo -u允许您从安装PostgreSQL时自动创建的postgres帐户运行命令。

Note: Since the authentication mode for PostgreSQL on Ubuntu 18.04 starts out as ident, by default an Ubuntu user can only operate in PostgreSQL with a role of the same name. For more information, check out the PostgreSQL official documentation on authentication.

注意:由于Ubuntu 18.04上PostgreSQL身份验证模式以ident启动,默认情况下,Ubuntu用户只能在具有相同角色的PostgreSQL中操作。 有关更多信息,请参阅PostgreSQL上有关身份验证的官方文档

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控制台:

  • sudo -u postgres psql

    须藤-u postgres psql

You will receive the following output, along with the prompt for the PostgreSQL console:

您将收到以下输出以及PostgreSQL控制台的提示:


   
   
Output
psql (10.9 (Ubuntu 10.9-0ubuntu0.18.04.1)) 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 sammy

    \密码萨米

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控制台:

  • \q

    \ q

Your usual prompt will now reappear.

现在,通常的提示将重新出现。

In this step, you created a new PostgreSQL role with super user privileges. Now you are ready to create a new Rails app that uses this role to create a database.

在此步骤中,您创建了具有超级用户权限的新PostgreSQL角色。 现在您可以创建一个新的Rails应用程序,该应用程序使用此角色来创建数据库。

步骤3 –创建一个新的Rails应用程序 (Step 3 – Creating a New Rails Application)

With a 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:

首先,导航到您的主目录:

  • cd ~

    光盘〜

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数据库。

步骤4 –配置和创建数据库 (Step 4 – Configuring and Creating Your Database)

When creating the development and test databases for your application, Rails will use the PostgreSQL role that you created for your Ubuntu username. To make sure that Rails creates these databases, you will alter the database configuration file of your project. You will then create your databases.

在为应用程序创建developmenttest数据库时,Rails将使用为Ubuntu用户名创建的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"' >> ~/.bashrc

    回声'导出APPNAME _DATABASE_PASSWORD =“ PostgreSQL_Role_Password ”'>>〜/ .bashrc

This command writes the export command to your ~/.bashrc file so that the environment variable will be set at login.

此命令将export命令写入您的~/.bashrc文件,以便在登录时设置环境变量。

To export the variable for your current session, use the source command:

要导出当前会话的变量,请使用source命令:

  • source ~/.bashrc

    来源〜/ .bashrc

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

  • nano config/database.yml

    纳米配置/数据库.yml

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 } %>并添加以下突出显示的行,以填充您的凭据和创建的环境变量。 它看起来应该像这样:

config/database.yml
config / database.yml
...
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: sammy
  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 + xY ,然后按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命令创建应用程序的数据库:

  • rails db:create

    rails db:create

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服务器中创建了一个developmenttest数据库。

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应用程序。 为确保您的应用程序正常运行,下一步是测试您的配置。

步骤5 –测试您的配置 (Step 5 – Testing Your Configuration)

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应用程序,使其在浏览器中显示。

Using the rails server command, run your web application on the built-in webserver in your Rails app, Puma:

使用rails server命令,在Rails应用程序Puma的内置Web服务器上运行Web应用程序:

  • rails server --binding=127.0.0.1

    Rails服务器--binding = 127.0.0.1

--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

  • curl http://127.0.0.1:3000

    卷曲http://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-linux) </p> </section> </div> </body> </html>

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, open port 3000 in your firewall:

如果您的Rails应用程序位于远程服务器上,并且您想通过Web浏览器访问它,则一种简单的方法是将其绑定到服务器的公共IP地址。 首先,在防火墙中打开端口3000

  • sudo ufw allow 3000

    须藤ufw允许3000

Next, look up the public IP address of your server. You can do this by running the following curl command:

接下来,查找服务器的公共IP地址。 您可以通过运行以下curl命令来执行此操作:

  • curl http://icanhazip.com

    卷曲http://icanhazip.com

This will return your public IP address. Use it with the rails server command, substituting server_public_IP with your server’s public IP:

这将返回您的公共IP地址。 与rails server命令一起使用,用服务器的公共IP替换server_public_IP

  • rails server --binding=server_public_IP

    Rails服务器--binding = server_public_IP

Now you will be able to access your Rails application in a local web browser via the server’s public IP address on port 3000 by visiting:

现在,您将能够通过访问端口3000上服务器的公共IP地址,在本地Web浏览器中访问Rails应用程序:

http://server_public_IP: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数据库。

After testing the configuration, if you would like to close port 3000, use the following command.

测试完配置后,如果您想关闭端口3000 ,请使用以下命令。

  • sudo ufw delete allow 3000

    sudo ufw删除允许3000

结论 (Conclusion)

In this tutorial, you created a Ruby on Rails web application that was configured to use PostgreSQL as a database on an Ubuntu 18.04 server. 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用作Ubuntu 18.04服务器上的数据库。 如果您想了解有关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的托管数据库产品

翻译自: https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-ruby-on-rails-application-on-ubuntu-18-04

rails中.bundle

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值