第一次如何配置postgresql? [英] How to configure postgresql for the first time?

查看:24
本文介绍了第一次如何配置postgresql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚安装了 postgresql 并且我在安装过程中指定了密码 x.当我尝试执行 createdb 并指定任何密码时,我收到消息:

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:

createdb:无法连接到数据库 postgres:致命:用户密码验证失败

createdb: could not connect to database postgres: FATAL: password authentication failed for user

createuser 相同.

我应该如何开始?我可以将自己作为用户添加到数据库吗?

How should I start? Can I add myself as a user to the database?

推荐答案

其他答案对我来说并不完全令人满意.以下是适用于 Xubuntu 12.04.1 LTS 上的 postgresql-9.1 的内容.

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

  1. 使用用户 postgres 连接到默认数据库:

  1. Connect to the default database with user postgres:

sudo -u postgres psql template1

sudo -u postgres psql template1

  • 为用户 postgres 设置密码,然后退出 psql (Ctrl-D):

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

    使用加密密码xxxxxxx"更改用户 postgres;

    ALTER USER postgres with encrypted password 'xxxxxxx';

  • 编辑pg_hba.conf文件:

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

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

    并在有关 postgres 的行上将peer"更改为md5":

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

    local          postgres   peer md5

    local      all     postgres     peer md5

    要了解您正在运行的 postgresql 版本,请查找 /etc/postgresql 下的 version 文件夹.此外,您可以使用 Nano 或其他编辑器代替 VIM.

    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.

    重启数据库:

    sudo/etc/init.d/postgresql restart

    sudo /etc/init.d/postgresql restart

    (在这里你可以检查它是否与 psql -U postgres 一起工作).

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

    创建一个与您同名的用户(要找到它,您可以输入whoami):

    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

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

    选项告诉 postgresql 创建一个可以登录、创建数据库、创建新角色的用户,是一个超级用户,并且会有一个加密的密码.真正重要的是 -P -E,要求您输入将被加密的密码,以及 -d 以便您可以执行 createdb.

    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.

    注意密码:它会首先询问您两次新密码(对于新用户),重复一次,然后是一次 postgres 密码(第 2 步中指定的密码).

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

    再次编辑 pg_hba.conf 文件(参见上面的第 3 步),并将所有"其他用户的行上的peer"更改为md5":

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

    local          所有   peer md5

    local      all     all     peer md5

  • 重新启动(如第 4 步),并检查您是否可以在不使用 -U postgres 的情况下登录:

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

    psql 模板1

    请注意,如果您仅执行 psql,它将失败,因为它会尝试将您连接到与您同名的默认数据库(即 whoami).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.

    现在 createdb 应该可以工作了.

    Now createdb <dbname> should work.

    这篇关于第一次如何配置postgresql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆