运维笔记 -- psql: FATAL: database "postgres" does not exist (postgres用户)

服务器终端,以postgres用户操作数据库,异常:

postgres@test:~$ psql
psql: FATAL: database "postgres" does not exist

处理方式:

方式一:psql终端执行:(这里是指:用类似Navicat工具连接执行,或者以psql template1临时用户连接) 然后执行如下命令。

CREATE USER postgres SUPERUSER;
select usename from pg_user;

 方式二:用知道的用户进入数据库【这里以搭建odoo docker环境时候,初始化数据库时的odoo用户为例】

psql -U odoo

 方式三: 以临时数据库方式进入

psql template1

CREATE USER postgres SUPERUSER;

补充:如果上方“方式三”命令不能正常执行,尝试如下操作:

确认谁在连接使用 template1: 

select * from pg_stat_activity where DATNAME = 'template1';

将相应连接进程关闭:

select pg_terminate_backend(1446); 

template1=# select * from pg_stat_activity where DATNAME = 'template1';
 datid |  datname  | pid  | usesysid | usename  | application_name |  client_addr   | client_hostname | client_port |         backend_start         |          xact_start           | 
         query_start          |         state_change          | waiting | state  | backend_xid | backend_xmin |                                                                       
   query                                                                          
-------+-----------+------+----------+----------+------------------+----------------+-----------------+-------------+-------------------------------+-------------------------------+-
------------------------------+-------------------------------+---------+--------+-------------+--------------+-----------------------------------------------------------------------
----------------------------------------------------------------------------------
     1 | template1 | 1693 |    17302 | postgres | psql             |                |                 |          -1 | 2021-01-25 09:23:03.940082+00 | 2021-01-25 09:23:39.727448+00 | 
2021-01-25 09:23:39.727448+00 | 2021-01-25 09:23:39.727452+00 | f       | active |             |      7713874 | select * from pg_stat_activity where DATNAME = 'template1';
     1 | template1 | 1446 |       10 | admin    |                  | 222.209.33.183 |                 |       57668 | 2021-01-25 08:49:10.710847+00 |                               | 
2021-01-25 08:49:22.730209+00 | 2021-01-25 08:49:22.755726+00 | f       | idle   |             |              | SELECT rolname, rolsuper, rolinherit, rolcreaterole, rolcreatedb, rolc
atupdate, rolcanlogin, rolconnlimit, rolvaliduntil, rolconfig, oid FROM pg_roles 
(2 rows)

template1=# SELECT pg_terminate_backend(1446);                           
 pg_terminate_backend 
----------------------
 t
(1 row)

然后再次执行创建数据库命令&授权命令:

template1=# CREATE DATABASE customs_data OWNER admin;                                                                                             
CREATE DATABASE

template1=# GRANT ALL PRIVILEGES ON DATABASE customs_data TO admin;
GRANT

其他情况处理:

https://www.cnblogs.com/maxiaohei/p/10321150.html

https://www.cnblogs.com/abclife/p/11636435.html

 

posted @ 2020-04-23 19:38  hello-Jesson  阅读(8419)  评论(0编辑  收藏  举报