Note: Open (PG)PostGreSQL in command line and do some stuffs

To access postgres console:

$ sudo -u postgres -i

postgres@host:~$ psql

or

$ sudo -u postgres psql

[sudo] password for john: 
psql (9.3.12)
Type "help" for help.

postgres=# 

 

# For help
postgres=# help
 You are using psql, the command-line interface to PostgreSQL.
Type: \copyright for distribution terms
 \h for help with SQL commands
 \? for help with psql commands
 \g or terminate with semicolon to execute query
 \q to quit
# For help
postgres=# \h
 Available help:
 ABORT CLUSTER DEALLOCATE END
 ALTER AGGREGATE COMMENT DECLARE EXECUTE
 ALTER COLLATION COMMIT DELETE EXPLAIN

Create database

# database command has to end with semicolon
postgres=# create database july_prod_dump1;
CREATE DATABASE

Alter permission

ALTER USER new_user CREATEDB

 

Rails : PostgreSQL : ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation

 

when invoking commands like

User.find(3).payment_sources.default.delete

I got this error

SQL (1.3ms) DELETE FROM "payment_sources" WHERE "payment_sources"."id" = $1 [["id", 1]]
PG::ForeignKeyViolation: ERROR: update or delete on table "payment_sources" violates foreign key constraint "fk_rails_7a7XXXd48" on table "pacts"
DETAIL: Key (id)=(1) is still referenced from table "pacts".
: DELETE FROM "payment_sources" WHERE "payment_sources"."id" = $1
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table "payment_sources" violates foreign key constraint "fk_rails_7a7XXXd48" on table "pacts"

 

Solution

class UpdateForeignkeyInfoInPacts < ActiveRecord::Migration
  def change
    add_foreign_key "pacts", "payment_sources", on_delete: :nullify
  end
end