PostgreSQL Database: PG::ConnectionBad: FATAL: Peer authentication failed for user

If, when working on a Linux machine of the Ubuntu flavor and setting up a PostgreSQL 9.3 database, you run into the error ‘PG::ConnectionBad: FATAL: Peer authentication failed for user’ when trying to connect to a database from a web application (Rails, PHP, Node, etc.), you are more than likely running into local socket connection permissions within PostgreSQL. The most common fix for this error in a development or staging environment is to loosen the local permissions up a bit.

How To:
1. locate the file ‘/etc/postgresql/9.3/main/pg_hba.conf’ and open it using sudo (sudo nano /etc/postgresql/9.3/main/pg_hba.conf)
2. scroll down through the file (almost to the bottom) until you find the section that starts with ‘# Database administrative login by Unix domain socket
3. directly below that you will find ‘local all all peer’ change it to ‘local all all trust’
4. save and close the file
5. restart the PostgreSQL server (sudo service postgresql restart)

Rails 4: gem install pg (when using the Postgresql App on OS X)

If you are getting a build error from Gem when trying to install the PostgreSQL gem called ‘pg’ and you are using the PostgreSQL App instead of installing PostgreSQL via Brew or some other method, it is because Gem cannot find the PostgreSQL config in the standard locations. To correct this you need to provide Gem with the path to the config file stored within the PostgreSQL App’s internal folder structure.

This can be done as follows:

gem install pg -- --with-pg-config=/Applications/Postgres.app/Contents/Versions/9.4/bin/pg_config

Another way to accomplish this is:

export CONFIGURE_ARGS=”with-pg-include=/Applications/Postgres.app/Contents/Versions/9.4/include/”
gem install pg

Note: the latest version of the PostgreSQL App, at the time of this writing was 9.4, your version may be different. Make sure you change the 9.4 in the config path in the command above to the one you are using or it will not work. You can verify the path by opening Finder, going to the Applications folder, locating Postgres.app, right clicking and selecting ‘show package contents’. This will open up the app’s folder structure in Finder.

Mint 14/PostgreSQL: Peer authentication failed for user "someuser"

After you have installed PostgreSQL 9.1 on your Linux Mint 12/13/14, created a user login, a database with that user as the owner and try to connect to that database using that user, you may run into the following error:
Password for user someuser:
psql
: FATAL:  Peer authentication failed for user “someuser”

If this is the case, you need to open up this file /etc/postgresql/9.1/main/pg_hba.conf in a text editor (with root privileges) and change this line:

local   all             all                                     peer

to this:

local   all             all                                     md5

After changing and saving the file, restart the PostgreSQL server:

sudo service postgresql restart
You should not have no issue connecting to your databases using the associated user.

MySQL: .Net Connector User Login Fails for Hashed but not ClearPasswords

If you are running into issues with the MySQL .Net Connector (any version) specially when it comes to user login after registration you may need to added some additional configuration to your web.config file.

I ran into a strange situation where a web application which was setup using the MySQL .Net Connector 6.3.0.0 for membership would allow a new user to register without an issue but once that user logs out and then attempts to log back in all they get is the “Your login attempt was unsuccessful” error message. I quadruple checked the web.config and made sure that the membership, roles, and profile providers for MySQL membership were all setup correctly. They were perfect! I finally in a ditch effort changed the password format from ‘Hashed’ to ‘Clear’, wiped out my membership database and recreated it. Amazingly, that fixed it, my users could register the same as before but this time they could actually login once logged out.

I did some Google searches on the strange difference between ‘Clear’ and ‘Hashed’ passwords and the MySQL .Net Connector and found that it relied heavily on the ‘MachineKey’ entry and was expecting a ‘ValidationKey’ and ‘DecryptionKey’ to be explicitly set. since I am running multiple applications on my production servers and since they are all using different keys in the farm I decided it would be best to place the ‘MachineKey’ entry in the web.config for each of my applications in order to allow them each to have their own keys.

Once I added the ‘MachineKey’ with the explicitly set values I switched the membership provider from ‘Clear’ to ‘Hashed’ password format, cleared out my membership database and retried registering and logging in users. This completely fixed my issue. Below is the entry that needs to be added to your web.config.

<machineKey
validationKey=”yourkeygoeshere”
decryptionKey=”yourkeygoeshere”
validation=”SHA1″
decryption=”AES”/>