How to configure postgresql for the first time?

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

    This is my solution:

    su root
    su postgres
    psql
    
    0 讨论(0)
  • 2020-11-28 17:34

    If you're running macOS like I am, you may not have the postgres user.

    When trying to run sudo -u postgres psql I was getting the error sudo: unknown user: postgres

    Luckily there are executables that postgres provides.

    createuser -D /var/postgres/var-10-local --superuser --username=nick
    createdb --owner=nick
    

    Then I was able to access psql without issues.

    psql
    psql (10.2)
    Type "help" for help.
    
    nick=#
    

    If you're creating a new postgres instance from scratch, here are the steps I took. I used a non-default port so I could run two instances.

    mkdir /var/postgres/var-10-local
    pg_ctl init -D /var/postgres/var-10-local
    

    Then I edited /var/postgres/var-10-local/postgresql.conf with my preferred port, 5433.

    /Applications/Postgres.app/Contents/Versions/10/bin/postgres -D /Users/nick/Library/Application\ Support/Postgres/var-10-local -p 5433
    
    createuser -D /var/postgres/var-10-local --superuser --username=nick --port=5433
    createdb --owner=nick --port=5433
    

    Done!

    0 讨论(0)
  • 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 <port>
      

      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.
    0 讨论(0)
  • 2020-11-28 17:44

    Just browse up to your installation's directory and execute this file "pg_env.bat", so after go at bin folder and execute pgAdmin.exe. This must work no doubt!

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

    In MacOS, I followed the below steps to make it work.

    For the first time, after installation, get the username of the system.

    $ cd ~
    $ pwd
    /Users/someuser
    $ psql -d postgres -U someuser
    

    Now that you have logged into the system, and you can create the DB.

    postgres=# create database mydb;
    CREATE DATABASE
    postgres=# create user myuser with encrypted password 'pass123';
    CREATE ROLE
    postgres=# grant all privileges on database mydb to myuser;
    GRANT
    
    0 讨论(0)
  • 2020-11-28 17:49
    Note: textdb is the database which you are going to explore with 'alex' user 
    
    root@kalilinux:~# sudo su - postgres 
    postgres=#  psql   
    postgres=#  create database testdb;
    postgres=#  create user alex with password 'alex';
    postgres=# GRANT ALL PRIVILEGES ON DATABASE testdb TO alex;`enter code here`
    
    0 讨论(0)
提交回复
热议问题