psql: FATAL: Peer authentication failed for user “dev”

后端 未结 12 819
盖世英雄少女心
盖世英雄少女心 2020-12-12 09:40

when i create a new user, but it cannot login the database.
I do that like this:

postgres@Aspire:/home/XXX$ createuser dev
Shall the new role be a superu         


        
相关标签:
12条回答
  • 2020-12-12 10:13

    pg_dump -h localhost -U postgres -F c -b -v -f mydb.backup mydb

    0 讨论(0)
  • 2020-12-12 10:15

    Your 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 dev and then login as dev or use sudo -u dev psql test_development 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=test_development --username=dev (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 if PostgreSQL is running you'll need to make it re-read the configuration by reloading (pg_ctl reload) or restarting (sudo service postgresql restart).

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

    Edited: Remarks from @Chloe, @JavierEH, @Jonas Eicher, @fccoelho, @Joanis, @Uphill_What comments incorporated into answer.

    0 讨论(0)
  • 2020-12-12 10:20

    For people in the future seeing this, postgres is in the /usr/lib/postgresql/10/bin on my Ubuntu server.

    I added it to the PATH in my .bashrc file, and add this line at the end

    PATH=$PATH:/usr/lib/postgresql/10/bin
    

    then on the command line

    $> source ./.bashrc
    

    I refreshed my bash environment. Now I can use postgres -D /wherever from any directory

    0 讨论(0)
  • 2020-12-12 10:21

    Try:

    psql -U role_name -d database -h hostname..com -W

    0 讨论(0)
  • 2020-12-12 10:28

    The easiest solution:

    CREATE USER dev WITH PASSWORD 'dev';
    CREATE DATABASE test_development;
    GRANT ALL PRIVILEGES ON DATABASE test_development to dev;
    ALTER ROLE dev CREATEROLE CREATEDB;
    
    0 讨论(0)
  • 2020-12-12 10:32

    This works for me when I run into it:

    sudo -u username psql
    
    0 讨论(0)
提交回复
热议问题