Ubuntu 16.04.3 设置 PostgreSQL

我在 ubuntu 16.04.3 下安装了 PostgreSQL 9.5, 按照之前的使用 PostgreSQL, 直接 su postgres 发现要输入密码, 我忘记了默认密码. 网上查阅后发现, ubuntu 下 postgres 用户是锁定的, 不建议用密码登陆. 但是我们可以直接用 sudo 来设置 psql 的用户和密码.

设置 postgres 数据库的密码用于管理:

sudo -u postgres psql postgres

其中 -u 是表示用用户 postgres, 后面的 psql 是 PostgreSQL shell 的程序, 打开 postgres 数据库. 命令输入之后, 在 psql 中设置 postgres 用户的密码:

postgres=# \password postgres
Enter new password:
Enter it again:

于是就设置成功.

接下来就是创建用户:

sudo -u postgres createuser --interactive
Enter name of role to add: wolfson
Shall the new role be a superuser? (y/n) n
Shall the new role be allowed to create databases? (y/n) n
Shall the new role be allowed to create more new roles? (y/n) n

然后是创建用户的数据库:

sudo -u postgres createdb wolfson

接着是设置用户的密码:

sudo -u postgres psql wolfson
wolfson=# \password wolfson
Enter new password:
Enter it again:

下面可以登陆新建的账户, 注意需要设置 --host=localhost, 否则会出现错误 psql: FATAL: Peer authentication failed for user "wolfson". 避免错误的另外一种方式创建一个 linux 用户为 wolfson.

psql -d wolfson -U wolfson --host=localhost

Password for user biostore:
psql (9.5.7)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.

biostore=>
By @Wolfson Liu in
Tags : #linux,