Fatal Error: Peer authentication failed for user "postgres", when trying to get pgsql working with rails.


Problem: Sometimes, we get the following error when trying to make a connection in Rails. 

FATAL ERROR: Peer authentication failed for user "postgres" 

Solution: There are a lot of solutions described below.

 

To changing pg_hba.conf file 

Change the following line

 

 

local   all             postgres                                peer

 

To

 

 

local   all             postgres                                md5

 

After altering the file, you have to restart your PostgreSQL service.

If you are on Linux, then use the following command.

 

 

$ sudo service postgresql restart

 

For Windows

 

 

Window--> Services--> PostgreSQL-x64-12--> Right clieck cand choose Re-Start option

 

 

If still, you are facing the same fatal error, then see the following solution.

1- Open the file pg_hba.conf (Location : /etc/postgresql/9.x/main)

2- Change the line

 

 

local   all             postgres                                peer

 

     To

 

 

local   all             postgres                                trust

 

 3- Restart the server

     For Linux, use the following command

 

 

$ sudo service postgresql restart

 

     For the window

 

 

Window--> Services--> PostgreSQL-x64-12--> Right clieck cand choose Re-Start option

 

     Login into pSQL and set the password

 

 

$ psql -U postgres

db> ALTER USER postgres with password 'your-pass';

 

 

    Finally, change the pg_hba.conf from

 

 

local   all             postgres                                trust

 


To

 

 

local   all             postgres                                md5

 

     Again restart the PostgreSQL Server

trust - No authorization anyone can access the database. Make sure don't leave the pSQL at this mode.

peer - Peer client operating system with the database user name to access it.

md5 - Authorized, Protected by password

Using the Command

If you don't want to change the config file(pg_hba.conf), try the following command to fix the issue.

 

 

root# su postgres

postgres$ psql -U postgres

psql (9.3.6)

Type "help" for help.

postgres=#\password

Enter new password:

Enter it again:

postgres=#

 

Or use the following

 

 

sudo psql --host=localhost --dbname=database-name --username=postgres

 


Also, try the following code in the connection.

 

 

Use host=localhost in connection.

PGconn *conn = PQconnectdb(

    "host=localhost user=postgres dbname=postgres password=123"

);

 


How to List columns with indexes in PostgreSQL?


Problem: I have a table of the customer below, and I want to list column with indexes in PostgreSQL, Basically purpose of this question is an analysis of the performance.

 

 

CREATE TABLE Customer

(

    Id serial NOT NULL,

    FName text COLLATE pg_catalog.default,

    LName text COLLATE pg_catalog.default,

    UserName text COLLATE pg_catalog.default,

    Password text COLLATE pg_catalog.default,

    Contact text COLLATE pg_catalog.default,

    Address text COLLATE pg_catalog.default,

    CONSTRAINT pk_Customer_Id PRIMARY KEY (Id)

)

 

 

Solution: Using pg_index we can list the column with index

 

select

    t.relname as table,

    i.relname as index,

    a.attname as column

from

    pg_class t,

    pg_class i,

    pg_index ix,

    pg_attribute a

where

    t.oid = ix.indrelid

    and i.oid = ix.indexrelid

    and a.attrelid = t.oid

    and a.attnum = ANY(ix.indkey)

    and t.relkind = 'r'

    and t.relname like 'Customer%'

order by

    t.relname,

    i.relname;

 


Using command \di

\di command is the easiest way to list all indexes in the current database.



As we know, command \d is used to list all the relations in the current database. \di is used to list the database indexes.

Note: But it will not show the column on which the index is created. Also, it will show all indexes, but not for a particular table.



NUnit : Introduction


There are several unit test tools in the market, but one of them is NUnit, the most popular tool for doing unit testing. So in this tutorial, we will learn the Unit Testing using the NUnit tool and will try to determine the reason for its popularity.

 

Unit Testing

 

The smallest unit of code is Function in an application: an application/software contains multiple Modules, and each Module composed various Classes and each class wrapped numerous Functions.

 

When we test individual function behavior without touching any other functions and determine whether it works exactly as per the requirements or not that is called Unit Testing.

 

Advantage of Unit Testing

 

1- Defects found early in development life cycle.

2- Reliable code.

3- Maintainable Code.

4- Faster testing by only single click of action.

 

NUnit

 

NUnit is the most used framework for writing unit test cases in the .NET. It supports both C# and VB.NET for coding, and it always suggests to write code in different assemblies that called Test Assemblies. These assemblies contain only testing code, nothing else. To check the test cases are failed or passed, run these test assemblies. For that, we required the Test Runner.

 

Test Runners is a UI tool that runs the NUnit test case and shows the test case results whether they have passed or failed. We will learn about test runners in the environment set-up in the next post.

 

NUnit can be used easily. There are some custom Attributes, and some Assert Classes are available in the NUnit tool that makes it easy to write unit tests.

 

Custom attributes provide a hint to NUnit test runners that these classes or functions contain unit testing code. Assert Classes use to test the conditions whether the system under test (SUT) satisfies a condition or not. If the condition met, then the test is pass else fail.

 

Some of the custom attributes are listed below: 

  • TestFixture
  • Setup
  • TearDown
  • Test
  • Category
  • Ignore
  • TestCase
  • Repeat
  • MaxTime


MongoDB Online Test 5


This MongoDB Online Test are the collection of popular questions asked in interviews for beginners/experienced. You will be presented Multiple Choice Questions (MCQs) based on MongoDB Concepts, where you will be given four options. You will select the answer to the question and then the next button to the next question. You will get an online test score and results with details after finishing the complete test.

Total Questions − 20
Max Time − 20 Min

MongoDB Online Test 4


This MongoDB Online Test are the collection of popular questions asked in interviews for beginners/experienced. You will be presented Multiple Choice Questions (MCQs) based on MongoDB Concepts, where you will be given four options. You will select the answer to the question and then the next button to the next question. You will get an online test score and results with details after finishing the complete test.

Total Questions − 20
Max Time − 20 Min

Fatal Error: Peer authentication failed for user "postgres", when trying to get pgsql working with rails.

Problem: Sometimes, we get the following error when trying to make a connection in Rails.  FATAL ERROR: Peer authentication failed for us...