服務器終端,以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