Createuser: could not connect to database postgres: FATAL: role “tom” does not exist

后端 未结 9 1176
醉话见心
醉话见心 2020-12-04 06:16

I\'m trying to set up Postgres for the first time, and I need to create a user with permissions to read and create databases. However, when I use:

createuser          


        
相关标签:
9条回答
  • 2020-12-04 06:19
    sudo -u postgres createuser -s tom 
    

    this should help you as this will happen if the administrator has not created a PostgreSQL user account for you. It could also be that you were assigned a PostgreSQL user name that is different from your operating system user name, in that case you need to use the -U switch.

    0 讨论(0)
  • 2020-12-04 06:20

    On Windows use:

    C:\PostgreSQL\pg10\bin>createuser -U postgres --pwprompt <USER>

    Add --superuser or --createdb as appropriate.

    See https://www.postgresql.org/docs/current/static/app-createuser.html for further options.

    0 讨论(0)
  • 2020-12-04 06:20

    You need to first run initdb. It will create the database cluster and the initial setup

    See How to configure postgresql for the first time? and http://www.postgresql.org/docs/8.4/static/app-initdb.html

    0 讨论(0)
  • 2020-12-04 06:31

    I had the same issue, i just do this

    sudo su - postgres

    createuser odoo -U postgres -dRSP #P for password (odoo or user name that you want o give the postgres access)

    0 讨论(0)
  • 2020-12-04 06:33

    You mentioned Ubuntu so I'm going to guess you installed the PostgreSQL packages from Ubuntu through apt.

    If so, the postgres PostgreSQL user account already exists and is configured to be accessible via peer authentication for unix sockets in pg_hba.conf. You get to it by running commands as the postgres unix user, eg:

    sudo -u postgres createuser owning_user
    sudo -u postgres createdb -O owning_user dbname
    

    This is all in the Ubuntu PostgreSQL documentation that's the first Google hit for "Ubuntu PostgreSQL" and is covered in numerous Stack Overflow questions.

    (You've made this question a lot harder to answer by omitting details like the OS and version you're on, how you installed PostgreSQL, etc.)

    0 讨论(0)
  • 2020-12-04 06:35

    See git gist with instructions here

    Run this:

     sudo -u postgres psql
    

    OR

    psql -U postgres
    

    in your terminal to get into postgres

    NB: If you're on a Mac and both of the commands above failed jump to the section about Mac below

    postgres=#
    

    Run

    CREATE USER new_username;
    

    Note: Replace new_username with the user you want to create, in your case that will be tom.

    postgres=# CREATE USER new_username;
    CREATE ROLE
    

    Since you want that user to be able to create a DB, you need to alter the role to superuser

    postgres=# ALTER USER new_username SUPERUSER CREATEDB;
    ALTER ROLE
    

    To confirm, everything was successful,

    postgres=# \du
                             List of roles
     Role name |                   Attributes                   | Member of 
    -----------+------------------------------------------------+-----------
    new_username     | Superuser, Create DB                           | {}
    postgres         | Superuser, Create role, Create DB, Replication | {}
    root             | Superuser, Create role, Create DB              | {}
    
    postgres=# 
    

    Update/Modification (For Mac):

    I recently encountered a similar error on my Mac:

    psql: FATAL: role "postgres" does not exist

    This was because my installation was setup with a database superuser whose role name is the same as your login (short) name.

    But some linux scripts assume the superuser has the traditional role name of postgres

    How did I resolve this?

    If you installed with homebrew run:

    /usr/local/opt/postgres/bin/createuser -s postgres

    If you're using a specific version of postgres, say 10.5 then run:

    /usr/local/Cellar/postgresql/10.5/bin/createuser -s postgres

    OR:

    /usr/local/Cellar/postgresql/10.5/bin/createuser -s new_username

    OR:

    /usr/local/opt/postgresql@11/bin/createuser -s postgres

    If you installed with postgres.app for Mac run:

    /Applications/Postgres.app/Contents/Versions/10.5/bin/createuser -s postgres

    P.S: replace 10.5 with your PostgreSQL version

    0 讨论(0)
提交回复
热议问题