<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Jitendra Chavhan</title>
    <description>The latest articles on Forem by Jitendra Chavhan (@jchavan1996).</description>
    <link>https://forem.com/jchavan1996</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1087073%2F3f6c097f-3914-4743-becf-02f9cd73c85a.jpeg</url>
      <title>Forem: Jitendra Chavhan</title>
      <link>https://forem.com/jchavan1996</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/jchavan1996"/>
    <language>en</language>
    <item>
      <title>How to use and integrate PostgreSQL to Rails</title>
      <dc:creator>Jitendra Chavhan</dc:creator>
      <pubDate>Thu, 01 Jun 2023 05:22:48 +0000</pubDate>
      <link>https://forem.com/jchavan1996/how-to-use-and-integrate-postgresql-to-rails-41k5</link>
      <guid>https://forem.com/jchavan1996/how-to-use-and-integrate-postgresql-to-rails-41k5</guid>
      <description>&lt;p&gt;Learn how to use PostgreSQL with your Ruby on Rails application, instead of the default SQLite database. SQLite is an easy-to-configure, lightweight product which ships with Ruby on Rails by default. However, PostgreSQL is a more robust solution which provides more advanced features, scaling, and stability, which may make it more suitable for your Ruby on Rails project.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Update the Gemfile&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Open the Gemfile which is locate in your root directory and add the following gem&lt;/p&gt;

&lt;p&gt;&lt;code&gt;gem 'pg'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Save this file &amp;amp; close it and run the bundle install command on your terminal&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Configure the database connection&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Open the database.yml file which is location in your root directories config folder and update the following configuration for development, test and production environments to use PostgreSQL.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;default: &amp;amp;default
  adapter: postgresql
  pool: &amp;lt;%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %&amp;gt;
  timeout: 5000
  host: localhost
  username: &amp;lt;%= ENV['PG_USERNAME'] %&amp;gt;
  password: &amp;lt;%= ENV['PG_PASSWORD'] %&amp;gt;
development:
  &amp;lt;&amp;lt;: *default
  database: app_development
test:
  &amp;lt;&amp;lt;: *default
  database: app_test

production:
  &amp;lt;&amp;lt;: *default
  database: app_production

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After updating the configuration of database.yml file save and close it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Create the database&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After updating the configuration, run the following command in your terminal to create the PostgreSQL database:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails db:create&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This command will create the database based on the configuration provided in database.yml.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Migrate the database&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you have existing migrations in your Rails application, you'll need to migrate the database to create the necessary tables in PostgreSQL. Run the following command:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails db:migrate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This will execute any pending migrations and update the PostgreSQL database schema.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: Verify the integration&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To ensure the integration was successful, start your Rails application using the rails server command and verify that it connects to the PostgreSQL database without any errors.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails s&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! You have successfully integrated PostgreSQL (pg) with your Rails application. You can now take advantage of PostgreSQL's advanced features and performance benefits in your Rails application.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>rails6</category>
      <category>postgres</category>
    </item>
  </channel>
</rss>
