Posts

Showing posts with the label pg

Swap primary keys between two records in the same table in Postgres

What if we need to swap two primary keys on the same table? What's the best and efficient way to do the same? let's check Now:- Let's assume we have a user table which has 3 columns among all the rows I have taken two sample rows which are the target rows for us. Id Name Email 101 Sabyasachi XX@gmail.com 105 Saghosh xxx@gmail.com We need to swap the primary keys (which are unique by nature) between those two records so our desired result will be something like   Id Name Email 105 Sabyasachi XX@gmail.com 101 Saghosh xxx@gmail.com Now lets buield a query using CTE(common table expression) to solve the problem:- with source_user as (   select     id   from     users   where     id = 101 ), destination_user as (   select     id   from     users   where     id

How To Setup Ruby on Rails with Postgres

Postgres is a great open source database management system. It's pretty easy to use postgres with Ruby on Rails. Step 1: Install postgress in your system. If you already have postgres installed in your system then skip it else follow the  link . Step 2: Create new project rails new pg_demo You can also create using rails new pg_demo --database=postgresql Step 3: include the pg gem in your gem file gem 'pg' Step 4: run bundle command bundle Step 5: It will give you some error like An error occurred while installing pg (0.17.1), and Bundler cannot continue. Make sure that `gem install pg -v '0.17.1'` succeeds before bundling. So let's try to install the  pg gem first gem install pg It will again throw an error  :( Building native extensions.  This could take a while... ERROR:  Error installing pg: ERROR: Failed to build gem native extension.     /home/sabyasachi/.rvm/rubies/ruby-2.1.0/bin/ruby extconf.rb checking for pg_config... y