1、
错误:psql: FATAL: Peer authentication failed for user "postgres"
解决办法如下:
1). 运行下面的命令编辑pg_hba.conf文件 sudo vim /etc/postgresql/9.1/main/pg_hba.conf
2). 将
# Database administrative login by Unix domain socket
local all postgres peer
改为
# Database administrative login by Unix domain socket
local all postgres trust
3). 保存后执行下面的命令重新加载配置文件: sudo /etc/init.d/postgresql reload
2、
错误:
conn = psycopg2.connect(database="testdb", user="postgres", password="nopasswd", host="127.0.0.1", port="5432")
File "/usr/lib/python2.7/dist-packages/psycopg2/__init__.py", line 179, in connect
connection_factory=connection_factory, async=async)
psycopg2.OperationalError: FATAL: password authentication failed for user "postgres"
FATAL: password authentication failed for user "postgres"
用jdbc连接Postgresql数据库时经常出现这个错误,这主要是由于用户密码认证方式引起的,Postgresql数据库安装好后默认采用md5密码加密认证方式。
解决方法:
1). 运行下面的命令编辑pg_hba.conf文件 sudo vim /etc/postgresql/9.1/main/pg_hba.conf
2). 将
# IPv4 local connections:
host all all 127.0.0.1/32 md5
更改为
# IPv4 local connections:
host all all 127.0.0.1/32 trust
3). 保存后执行下面的命令重新加载配置文件: sudo /etc/init.d/postgresql reload
3、
错误:
create index for some fields
psql: FATAL: Ident authentication failed for user "root"
解决方法:
1). 运行下面的命令编辑pg_hba.conf文件 sudo vim /etc/postgresql/9.1/main/pg_hba.conf
2). 将
# "local" is for Unix domain socket connections only
local all all peer //旧版本该处为ident
更改为
# "local" is for Unix domain socket connections only
local all all trust
3). 保存后执行下面的命令重新加载配置文件: sudo /etc/init.d/postgresql reload
4
、psql: FATAL: role root does not exist
在postgresql中创建一个名为root的角色即可:
postgres@debian:~$ createuser root
postgres@debian:~$ psql
psql (9.3.3)
Type "help" for help.
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
long | | {}
postgres | Superuser, Create role, Create DB, Replication | {}
root | | {}
创建完角色之后如果出现以下错误:
createdb: database creation failed: ERROR: permission denied to create database
你需要为postgresql进行一些操作的授权(并非所有用户都经过了创建新数据库的授权。)
在创建角色时就赋予角色一些属性,可以使用下面的方法。
首先切换到postgres 用户。
创建角色bella 并赋予其CREATEDB 的权限。
postgres=# CREATE ROLE bella CREATEDB ;
CREATE ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------+-----------
bella | Create DB, Cannot login | {}
david | | {}
postgres | Superuser, Create role, Create DB, Replication | {}
sandy | | {}
创建角色并赋予多个属性时属性之间要用空格:
postgres=# create role bellaaa CREATEDB superuser Createrole Replication login; //要添加的属性一般要包括如下。
CREATE ROLE
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+-------------------------------------------------+-----------
bella | Superuser, Create DB, Cannot login | {}
bellaa | Superuser, Create role, Create DB, Cannot login | {}
bellaaa | Superuser, Create role, Create DB, Replication| {}
postgres | Superuser, Create role, Create DB, Replication | {}
root | Superuser, Create DB | {}
5.在执行以下操作后提示FATAL: role 'root' is not permitted to log in.
后直接将postgres所有权限都给root后,仍报错
google后,由于create role时不为用户赋予login权限,赋予login权限即可
postgres=# alter user root login;
ALTER ROLE
6.
没有设置PGDATABASE这个环境变量时,使用psql进行登录,默认的 数据库是与操作 系统用户名一致的,这时候会报错:
wangye@selfimpro:~$ psql
psql: FATAL: database "wangye" does not exist
然后设置(export)环境变量PGDATABASE=testDB,这样就默认登录wangye数据库:
wangye@selfimpro:~$ su postgres
Password:
postgres@selfimpro:/home/wangye$ psql
psql (9.1.14)
Type "help" for help.
postgres@selfimpro:/home/wangye$ createdb wangye -E utf-8
postgres@selfimpro:/home/wangye$ psql wangye
psql (9.1.14)
Type "help" for help.