How To Install Postgresql With Homebrew
Introduction
When using the Ruby on Runway web framework, your application is prepare by default to use SQLite every bit a database. SQLite is a lightweight, portable, and user-friendly relational database that performs peculiarly well in depression-memory environments, and will work well in many cases. However, for highly complex applications that need more reliable information integrity and programmatic extensibility, a PostgreSQL database volition be a more robust and flexible option. In lodge to configure your Ruby on Runway setup to employ PostgreSQL, you volition need to perform a few boosted steps to get information technology upward and running.
In this tutorial, you will set up a Ruby on Runway development surround continued to a PostgreSQL database on a local macOS machine. You will install and configure PostgreSQL, and then examination your setup by creating a Rails application that uses PostgreSQL as its database server.
Prerequisites
This tutorial requires the following:
-
1 calculator or virtual machine with macOS installed, with authoritative access to that car and an internet connection. This tutorial has been tested on macOS x.14 Mojave.
-
A Reddish on Track development environment installed on your macOS machine. To set this upwards, follow our guide on How To Install Ruby on Track with rbenv on macOS. This tutorial will use version 2.6.3 of Reddish and 5.2.iii of Rail; for information on the latest versions, check out the official sites for Blood-red and Rails.
Step 1 — Installing PostgreSQL
In lodge to configure Crimson on Runway to create your spider web application with PostgreSQL as a database, you will first install the database onto your motorcar. Although in that location are many means to install PostgreSQL on macOS, this tutorial will utilize the package manager Homebrew.
There are multiple Homebrew packages to install different versions of PostgreSQL. To install the latest version, run the following command:
- brew install postgresql
If you lot would similar to download a specific version of PostgreSQL, supercede postgresql
in the previous command with your desired package. You lot can find the available packages at the Homebrew website.
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:
- echo 'export PATH="/usr/local/opt/postgresql@x/bin:$PATH"' >> ~/.bash_profile
And so, apply the changes you fabricated to your ~/.bash_profile
file to your current shell session:
- source ~/.bash_profile
To start the service and enable information technology to start at login, run the following:
- brew services start postgresql@10
Bank check to make certain the installation was successful:
- postgres -5
Yous will go the following output:
Output
postgres (PostgreSQL) 10.9
Once PostgreSQL is installed, the next footstep is to create a function that your Rails application will use later to create your database.
Step 2 — Creating a Database Part for Your Awarding
In PostgreSQL, roles tin can be used to organize permissions and authorization. When starting PostgreSQL with Homebrew, y'all will automatically accept a superuser part created with your macOS username. In order to keep these superuser privileges split from the database instance you use for your Runway application, in this step you will create a new role with less access.
To create a new role, run the following control, replacing appname
with whatever name you'd like to give the function:
- 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.
You also specified the -P
flag, which means you lot will exist prompted to enter a countersign for your new role. Enter your desired password, making sure to record information technology and then that you can apply it in a configuration file in a hereafter step.
If you did non utilize the -P
flag and desire to set a countersign for the role subsequently yous create it, enter the PostgreSQL panel with the post-obit control:
- psql postgres
Yous will receive the following output, along with the prompt for the PostgreSQL panel:
Output
psql (10.ix) Type "assist" for help. postgres=#
The PostgreSQL console is indicated by the postgres=#
prompt. At the PostgreSQL prompt, enter this command to ready the countersign for the new database function, replacing the highlighted proper name with the one you lot created:
- \password appname
PostgreSQL will prompt you for a countersign. Enter your desired password at the prompt, then confirm information technology.
At present, exit the PostgreSQL console by inbound this control:
- \q
Your usual prompt will now reappear.
In this step, you created a new PostgreSQL function without superuser privileges for your application. Now you are ready to create a new Rails app that uses this role to create a database.
Footstep iii — Creating a New Rails Application
With your role configured for PostgreSQL, yous tin now create a new Runway application that is fix to use PostgreSQL equally a database.
Commencement, navigate to your home directory:
- cd ~
Create a new Rails application in this directory, replacing appname
with any you would like to telephone call your app:
- rails new appname -d=postgresql
The -d=postgresql
choice sets PostgreSQL as the database.
Once you've run this command, a new folder named appname
volition appear in your home directory, containing all the elements of a basic Runway application.
Next, move into the application'southward directory:
- cd appname
Now that y'all take created a new Rails application and have moved into the root directory for your project, y'all can configure and create your PostgreSQL database from within your Runway app.
Pace four — Configuring and Creating Your Database
When creating the development
and test
databases for your application, Track will use the PostgreSQL role that you created in Step 2. To brand sure that Rails creates these databases, you lot volition change the database configuration file of your project. You lot will then create your databases.
One of the configuration changes to make in your Runway application is to add together the password for the PostgreSQL role you created in the last stride. To keep sensitive information similar passwords safe, it is a practiced idea to shop this in an environs variable rather than to write it directly in your configuration file.
To store your password in an environs variable at login, run the following command, replacing APPNAME
with the proper name of your app and PostgreSQL_Role_Password
with the password you created in the last step:
- echo 'export APPNAME_DATABASE_PASSWORD="PostgreSQL_Role_Password"' >> ~/.bash_profile
This command writes the export
control to your ~/.bash_profile
file so that the environment variable volition be set at login.
To consign the variable for your current session, employ the source
command:
- source ~/.bash_profile
Now that you lot have stored your password in your environment, it's time to alter the configuration file.
Open up your application's database configuration file in your preferred text editor. This tutorial will utilize nano
:
- nano config/database.yml
Under the default
department, find the line that says pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
and add the post-obit highlighted lines, filling in your credentials and the environment variable you created. It should look something like this:
config/database.yml
... default: &default adapter: postgresql encoding: unicode # For details on connection pooling, see Track configuration guide # http://guides.rubyonrails.org/configuring.html#database-pooling puddle: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> username: appname countersign: <%= ENV['APPNAME_DATABASE_PASSWORD'] %> development: <<: *default database: appname_development ...
This will make the Rails application run the database with the correct role and countersign. Save and exit by pressing CTRL+X
, Y
, then ENTER
.
For more information on configuring databases in Rails, run into the Rails documentation.
At present that y'all accept made changes to config/database.yml
, create your application'south databases by using the rails
command:
- rails db:create
In one case Rails creates the database, you lot volition receive the following output:
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.
You now accept a PostgreSQL database connected to your Rails app. To ensure that your application is working, the next step is to examination your configuration.
Step 5 — Testing Your Configuration
To exam that your awarding is able to use the PostgreSQL database, try to run your web application and so that information technology will evidence upward in a browser.
Get-go, you'll utilise the built-in spider 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 post-obit control:
- rails server --bounden= 127.0.0.1
--binding
binds your awarding to a specified IP. By default, this flag volition bind Runway to 0.0.0.0
, but since this means that Rails will mind to all interfaces, it is more secure to utilize 127.0.0.one
to specify the localhost
. Past default, the awarding listens on port 3000
.
Once your Rails app is running, your control prompt will disappear, replaced past this output:
Output
=> Booting Puma => Rails 5.2.3 application starting in development => Run `rails server -h` for more startup options Puma starting in single style... * Version 3.12.ane (red 2.6.3-p62), codename: Llamas in Pajamas * Min threads: 5, max threads: v * Environment: development * Listening on tcp://127.0.0.i:3000 Utilise Ctrl-C to stop
To test if your application is running, open up a new terminal window on your server and employ the curl
command to send a request to 127.0.0.1:3000
:
- whorl http://127.0.0.1:3000
You will receive a lot of output in HTML, ending in something like:
Output
... <stiff>Rails version:</potent> 5.2.3<br /> <potent>Ruby version:</stiff> 2.6.iii (x86_64-darwin18) </p> </section> </div> </trunk> </html>
You can likewise access your Rails application in a local spider web browser by visiting:
http://127.0.0.one:3000
At this URL, y'all will find a Ruby on Rails welcome folio:
This means that your awarding is properly configured and connected to the PostgreSQL database.
Conclusion
In this tutorial, you created a Ruby on Rails spider web application that was configured to use PostgreSQL as a database on a local macOS machine. If you would like to larn more near the Ruby programming language, bank check out our How To Code in Cherry-red series.
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 than about how to apply databases, see our An Introduction to Queries in PostgreSQL article, or explore DigitalOcean'due south Managed Databases production.
Source: https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-ruby-on-rails-application-on-macos
Posted by: moorehichat.blogspot.com
0 Response to "How To Install Postgresql With Homebrew"
Post a Comment