How to configure postgresql for the first time?

前端 未结 10 826
太阳男子
太阳男子 2020-11-28 16:54

I have just installed postgresql and I specified password x during installation. When I try to do createdb and specify any password I get the message:

相关标签:
10条回答
  • 2020-11-28 17:54

    EDIT: Warning: Please, read the answer posted by Evan Carroll. It seems that this solution is not safe and not recommended.

    This worked for me in the standard Ubuntu 14.04 64 bits installation.

    I followed the instructions, with small modifications, that I found in http://suite.opengeo.org/4.1/dataadmin/pgGettingStarted/firstconnect.html

    1. Install postgreSQL (if not already in your machine):

    sudo apt-get install postgresql

    1. Run psql using the postgres user

    sudo –u postgres psql postgres

    1. Set a new password for the postgres user:

    \password postgres

    1. Exit psql

    \q

    1. Edit /etc/postgresql/9.3/main/pg_hba.conf and change:

    #Database administrative login by Unix domain socket local all postgres peer

    To:

    #Database administrative login by Unix domain socket local all postgres md5

    1. Restart postgreSQL:

    sudo service postgresql restart

    1. Create a new database

    sudo –u postgres createdb mytestdb

    1. Run psql with the postgres user again:

    psql –U postgres –W

    1. List the existing databases (your new database should be there now):

    \l

    0 讨论(0)
  • 2020-11-28 17:56

    The other answers were not completely satisfying to me. Here's what worked for postgresql-9.1 on Xubuntu 12.04.1 LTS.

    1. Connect to the default database with user postgres:

      sudo -u postgres psql template1

    2. Set the password for user postgres, then exit psql (Ctrl-D):

      ALTER USER postgres with encrypted password 'xxxxxxx';

    3. Edit the pg_hba.conf file:

      sudo vim /etc/postgresql/9.1/main/pg_hba.conf

      and change "peer" to "md5" on the line concerning postgres:

      local      all     postgres     peer md5

      To know what version of postgresql you are running, look for the version folder under /etc/postgresql. Also, you can use Nano or other editor instead of VIM.

    4. Restart the database :

      sudo /etc/init.d/postgresql restart

      (Here you can check if it worked with psql -U postgres).

    5. Create a user having the same name as you (to find it, you can type whoami):

      sudo createuser -U postgres -d -e -E -l -P -r -s <my_name>

      The options tell postgresql to create a user that can login, create databases, create new roles, is a superuser, and will have an encrypted password. The really important ones are -P -E, so that you're asked to type the password that will be encrypted, and -d so that you can do a createdb.

      Beware of passwords: it will first ask you twice the new password (for the new user), repeated, and then once the postgres password (the one specified on step 2).

    6. Again, edit the pg_hba.conf file (see step 3 above), and change "peer" to "md5" on the line concerning "all" other users:

      local      all     all     peer md5

    7. Restart (like in step 4), and check that you can login without -U postgres:

      psql template1

      Note that if you do a mere psql, it will fail since it will try to connect you to a default database having the same name as you (i.e. whoami). template1 is the admin database that is here from the start.

    8. Now createdb <dbname> should work.

    0 讨论(0)
  • 2020-11-28 17:58

    Under Linux PostgresQL is usually configured to allow the root user to login as the postgres superuser postgres from the shell (console or ssh).

    $ psql -U postgres
    

    Then you would just create a new database as usual:

    CREATE ROLE myuser LOGIN password 'secret';
    CREATE DATABASE mydatabase ENCODING 'UTF8' OWNER myuser;
    

    This should work without touching pg_hba.conf. If you want to be able to do this using some GUI tool over the network - then you would need to mess with pg_hba.conf.

    0 讨论(0)
  • 2020-11-28 17:58

    You probably need to update your pg_hba.conf file. This file controls what users can log in from what IP addresses. I think that the postgres user is pretty locked-down by default.

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