postgresal使用錯誤解決 (記錄篇)

桁桁8道發表於2014-04-15

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.

相關文章