Skip to content

Instantly share code, notes, and snippets.

@decodedmrq
Forked from AtulKsol/psql-error-fix.md
Created March 3, 2021 16:42
Show Gist options
  • Save decodedmrq/1a00b8d833d466f791876b46bda11a03 to your computer and use it in GitHub Desktop.
Save decodedmrq/1a00b8d833d466f791876b46bda11a03 to your computer and use it in GitHub Desktop.

Revisions

  1. @AtulKsol AtulKsol revised this gist Apr 25, 2020. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions psql-error-fix.md
    Original file line number Diff line number Diff line change
    @@ -46,10 +46,11 @@ You can, of course, also create more specific rules for a specific database or u

    5. Revert the changes in `pg_hba.conf` file from `trust` to `md5` and restart `postgresql`.

    <sup>* The file `pg_hba.conf` will most likely be at `/etc/postgresql/9.x/main/pg_hba.conf`
    ##### pg_hba.conf file location

    To check location of pg_hba.conf connect to postgres db using psql then type SHOW hba_file; command.
    The file `pg_hba.conf` will most likely be at `/etc/postgresql/9.x/main/pg_hba.conf`
    To check location of pg_hba.conf connect to postgres db using psql then type `SHOW hba_file;` command.

    After change pg_hba.conf file, you can execute `SELECT pg_reload_conf();` or pg_ctl reload with superuser instead of restart postgresql service.</sup>
    After change pg_hba.conf file, you can execute `SELECT pg_reload_conf();` or pg_ctl reload with superuser instead of restart postgresql service.

    <sup>* [Source](http://stackoverflow.com/a/21889759/2945616) </sup>
  2. @AtulKsol AtulKsol revised this gist Apr 25, 2020. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion psql-error-fix.md
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,10 @@ You can, of course, also create more specific rules for a specific database or u

    5. Revert the changes in `pg_hba.conf` file from `trust` to `md5` and restart `postgresql`.

    <sup>* The file `pg_hba.conf` will most likely be at `/etc/postgresql/9.x/main/pg_hba.conf`

    <sup>* The file `pg_hba.conf` will most likely be at `/etc/postgresql/9.x/main/pg_hba.conf`</sup>
    To check location of pg_hba.conf connect to postgres db using psql then type SHOW hba_file; command.

    After change pg_hba.conf file, you can execute `SELECT pg_reload_conf();` or pg_ctl reload with superuser instead of restart postgresql service.</sup>

    <sup>* [Source](http://stackoverflow.com/a/21889759/2945616) </sup>
  3. @AtulKsol AtulKsol revised this gist Jan 29, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion psql-error-fix.md
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,9 @@ to

    You can, of course, also create more specific rules for a specific database or user, with some users having `peer` and others requiring passwords.

    #### After changing `pg_hba.conf` you'll need to restart PostgreSQL if it's running. E.g. `sudo service postgresql restart`
    > After changing `pg_hba.conf` you'll need to restart PostgreSQL if it's running. E.g. `sudo service postgresql restart`


    ##### Steps to change/create default `postgres` user's password:

  4. @AtulKsol AtulKsol revised this gist Jan 29, 2017. 1 changed file with 22 additions and 1 deletion.
    23 changes: 22 additions & 1 deletion psql-error-fix.md
    Original file line number Diff line number Diff line change
    @@ -20,9 +20,30 @@ to

    - `md5` means it will always ask for a password, and validate it after hashing with `MD5`.

    - `trust` means it will never ask for a password, and always trust any connection.

    You can, of course, also create more specific rules for a specific database or user, with some users having `peer` and others requiring passwords.

    After changing `pg_hba.conf` you'll need to restart PostgreSQL if it's running. E.g. `sudo service postgresql restart`
    #### After changing `pg_hba.conf` you'll need to restart PostgreSQL if it's running. E.g. `sudo service postgresql restart`

    ##### Steps to change/create default `postgres` user's password:

    1. `trust` connection by adding in `pg_hba.conf` file

    - `local all postgres trust`

    2. Restart postgresql service

    - `sudo service postgresql restart`

    3. `psql -U postgres`

    4. At the `postgres=#` prompt, change the user name `postgres` password:

    - `ALTER USER postgres with password ‘new-password’;`

    5. Revert the changes in `pg_hba.conf` file from `trust` to `md5` and restart `postgresql`.


    <sup>* The file `pg_hba.conf` will most likely be at `/etc/postgresql/9.x/main/pg_hba.conf`</sup>

  5. @AtulKsol AtulKsol revised this gist Dec 20, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions psql-error-fix.md
    Original file line number Diff line number Diff line change
    @@ -25,4 +25,5 @@ You can, of course, also create more specific rules for a specific database or u
    After changing `pg_hba.conf` you'll need to restart PostgreSQL if it's running. E.g. `sudo service postgresql restart`

    <sup>* The file `pg_hba.conf` will most likely be at `/etc/postgresql/9.x/main/pg_hba.conf`</sup>

    <sup>* [Source](http://stackoverflow.com/a/21889759/2945616) </sup>
  6. @AtulKsol AtulKsol revised this gist Dec 20, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion psql-error-fix.md
    Original file line number Diff line number Diff line change
    @@ -25,5 +25,4 @@ You can, of course, also create more specific rules for a specific database or u
    After changing `pg_hba.conf` you'll need to restart PostgreSQL if it's running. E.g. `sudo service postgresql restart`

    <sup>* The file `pg_hba.conf` will most likely be at `/etc/postgresql/9.x/main/pg_hba.conf`</sup>

    <sup>* [Source](http://stackoverflow.com/a/21889759/2945616) </sup>
  7. @AtulKsol AtulKsol created this gist Dec 20, 2016.
    29 changes: 29 additions & 0 deletions psql-error-fix.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    ## psql: FATAL: Peer authentication failed for user “postgres” (or any user)

    The connection failed because by default `psql` connects over UNIX sockets using `peer` authentication, that requires the current UNIX user to have the same user name as `psql`. So you will have to create the UNIX user `postgres` and then login as `postgres` or use `sudo -u postgres psql database-name` for accessing the database (and `psql` should **not** ask for a password).

    If you cannot or do not want to create the UNIX user, like if you just want to connect to your database for *ad hoc* queries, forcing a socket connection using `psql --host=localhost --dbname=database-name --username=postgres` (as pointed out by @meyerson answer) will solve your immediate problem.

    But if you intend to force password authentication over Unix sockets instead of the peer method, try changing the following `pg_hba.conf`* line:

    from

    # TYPE DATABASE USER ADDRESS METHOD
    local all all peer

    to

    # TYPE DATABASE USER ADDRESS METHOD
    local all all md5

    - `peer` means it will trust the identity (authenticity) of UNIX user. So not asking for a password.

    - `md5` means it will always ask for a password, and validate it after hashing with `MD5`.

    You can, of course, also create more specific rules for a specific database or user, with some users having `peer` and others requiring passwords.

    After changing `pg_hba.conf` you'll need to restart PostgreSQL if it's running. E.g. `sudo service postgresql restart`

    <sup>* The file `pg_hba.conf` will most likely be at `/etc/postgresql/9.x/main/pg_hba.conf`</sup>

    <sup>* [Source](http://stackoverflow.com/a/21889759/2945616) </sup>