How to configure postgresql for the first time?

前端 未结 10 854
太阳男子
太阳男子 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:35

    There are two methods you can use. Both require creating a user and a database.

    1. Using createuser and createdb,

      $ sudo -u postgres createuser --superuser $USER
      $ createdb mydatabase
      $ psql -d mydatabase
      
    2. Using the SQL administration commands, and connecting with a password over TCP

      $ sudo -u postgres psql postgres
      

      And, then in the psql shell

      CREATE ROLE myuser LOGIN PASSWORD 'mypass';
      CREATE DATABASE mydatabase WITH OWNER = myuser;
      

      Then you can login,

      $ psql -h localhost -d mydatabase -U myuser -p 
      

      If you don't know the port, you can always get it by running the following, as the postgres user,

      SHOW port;
      

      Or,

      $ grep "port =" /etc/postgresql/*/main/postgresql.conf
      

    Sidenote: the postgres user

    I suggest NOT modifying the postgres user.

    1. It's normally locked from the OS. No one is supposed to "log in" to the operating system as postgres. You're supposed to have root to get to authenticate as postgres.
    2. It's normally not password protected and delegates to the host operating system. This is a good thing. This normally means in order to log in as postgres which is the PostgreSQL equivalent of SQL Server's SA, you have to have write-access to the underlying data files. And, that means that you could normally wreck havoc anyway.
    3. By keeping this disabled, you remove the risk of a brute force attack through a named super-user. Concealing and obscuring the name of the superuser has advantages.

提交回复
热议问题