如何登录和验证后,一个新的安装?

在薄荷 Ubuntu 上重新安装了 postgres 8.4。如何创建 postgres 用户并使用 psql 登录?

当我输入 psql 时,它只是告诉我

psql: FATAL: Ident authentication failed for user "my-ubuntu-username"
236616 次浏览

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

默认情况下,psql 使用与用户相同的名称连接到数据库。因此,有一个约定,使 "user's database"。如果您的用户只需要一个数据库,那么没有理由打破这个约定。我们将使用 mydatabase作为示例数据库名称。

  1. 使用 createuser 和 createdb ,我们可以清楚地说明数据库名称,

    $ sudo -u postgres createuser -s $USER
    $ createdb mydatabase
    $ psql -d mydatabase
    

    您可能应该完全忽略这一点,而是让所有命令默认为用户名。

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

    $ sudo -u postgres psql postgres
    

    然后在 psql shell 中

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

    然后你就可以登录了,

    $ psql -h localhost -d mydatabase -U myuser -p <port>
    

    如果您不知道端口,可以通过运行以下命令获得端口,作为 postgres用户,

    SHOW port;
    

    或者,

    $ 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.

by default you would need to use the postgres user:

sudo -u postgres psql postgres

您得到的错误是因为 your-ubuntu-username 不是一个有效的 Postgres 用户。

您需要告诉 psql 要使用哪个数据库用户名

psql -U postgres

您可能还需要指定要连接到的数据库

psql -U postgres -d <dbname>

如果您的数据库客户端连接到 TCP/IP,并且您已经在 pg _ hba 中配置了标识身份验证。Conf 检查您是否安装并运行了标识。即使您只有本地客户端连接到“ localhost”,这也是强制性的。

另外要注意的是,现在的 identd 可能必须是 启用 IPv6,以便 Postgreql 欢迎连接到本地主机的客户机。

您还可以作为“普通”用户(而不是 postgres)连接到数据库:

postgres=# \connect opensim Opensim_Tester localhost;


Password for user Opensim_Tester:


You are now connected to database "opensim" as user "Opensim_Tester" on host "localhost" at port "5432"

使用 postgres 用户或我们创建的任何其他用户登录的主要区别在于,当使用 postgres 用户时,不需要使用 - 哦指定主机,而是为另一个用户指定。

用 postgres 用户登录

$ psql -U postgres

创建并与其他用户一起登录

# CREATE ROLE usertest LOGIN PASSWORD 'pwtest';
# CREATE DATABASE dbtest WITH OWNER = usertest;
# SHOW port;
# \q


$ psql -h localhost -d dbtest -U usertest -p 5432

GL

来源

使用 下面的命令,每个人都可以在安装后登录 PostgreSQL。 * 默认数据库 postgres使用:

psql -U postgres

在运行 上面的命令之后,你需要放置 密码:

Password for user postgres: