全新安装后如何登录 Postgresql 并进行身份验证? [英] How do I login and authenticate to Postgresql after a fresh install?
问题描述
在 mint ubuntu 上新安装了 postgres 8.4.如何为 postgres 创建用户并使用 psql 登录?
Did a new install of postgres 8.4 on mint ubuntu. How do I create a user for postgres and login using psql?
当我输入 psql 时,它只是告诉我
When I type psql, it just tells me
psql: FATAL: Ident authentication failed for user "my-ubuntu-username"
推荐答案
您可以使用两种方法.两者都需要创建一个用户和一个数据库.
There are two methods you can use. Both require creating a user and a database.
默认情况下,psql 连接到与用户同名的数据库.所以有一个约定使用户的数据库".如果您的用户只需要一个数据库,则没有理由打破这一约定.我们将使用 mydatabase
作为示例数据库名称.
By default psql connects to the database with the same name as the user. So there is a convention to make that the "user's database". And there is no reason to break that convention if your user only needs one database. We'll be using mydatabase
as the example database name.
使用 createuser 和 createdb,我们可以明确数据库名称,
Using createuser and createdb, we can be explicit about the database name,
$ sudo -u postgres createuser -s $USER
$ createdb mydatabase
$ psql -d mydatabase
您可能应该完全忽略这一点,而是让所有命令默认为用户名.
You should probably be omitting that entirely and letting all the commands default to the user's name instead.
$ sudo -u postgres createuser -s $USER
$ createdb
$ psql
使用 SQL 管理命令,并通过 TCP 使用密码进行连接
$ sudo -u postgres psql postgres
然后在 psql shell 中
And, then in the psql shell
CREATE ROLE myuser LOGIN PASSWORD 'mypass';
CREATE DATABASE mydatabase WITH OWNER = myuser;
然后就可以登录了,
$ psql -h localhost -d mydatabase -U myuser -p <port>
如果你不知道端口,你总是可以通过运行以下命令来获取它,作为 postgres
用户,
If you don't know the port, you can always get it by running the following, as the postgres
user,
SHOW port;
或者,
$ grep "port =" /etc/postgresql/*/main/postgresql.conf
旁注:postgres
用户
我建议不要修改postgres
用户.
- 它通常被操作系统锁定.没有人应该以
postgres
的身份登录"到操作系统.您应该拥有 root 权限才能以postgres
身份进行身份验证. - 它通常不受密码保护并委托给主机操作系统.这是好事.这通常意味着为了以
postgres
的身份登录,PostgreSQL 相当于 SQL Server 的SA
,您必须具有对底层数据文件的写访问权限.而且,这意味着你通常可以破坏破坏. - 通过禁用此功能,您可以消除通过指定超级用户进行暴力攻击的风险.隐藏和隐藏超级用户的名称具有优势.
- 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 aspostgres
. - 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'sSA
, you have to have write-access to the underlying data files. And, that means that you could normally wreck havoc anyway. - 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.
这篇关于全新安装后如何登录 Postgresql 并进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!