首页 > 解决方案 > psql 说数据库不存在但它确实存在于 pgadmin

问题描述

我刚刚在 Windows 上安装了 Postgres v10.4,并使用 pgadmin 创建了一个名为 analysis 的新数据库。它就在那里,我可以在 pgadmin 中看到它,并且它有一个表。但是,我无法使用 psql 连接到该数据库。

在此处输入图像描述

C:\WINDOWS\system32>psql -d postgres -U postgres
psql (10.4)
WARNING: Console code page (850) differs from Windows code page (1252)
         8-bit characters might not work correctly. See psql reference
         page "Notes for Windows users" for details.
Type "help" for help.

postgres=# l
postgres-# \l
                                                  List of databases
   Name    |  Owner   | Encoding |           Collate           |            Ctype            |   Access privileges
-----------+----------+----------+-----------------------------+-----------------------------+-----------------------
 postgres  | postgres | UTF8     | English_United Kingdom.1252 | English_United Kingdom.1252 |
 template0 | postgres | UTF8     | English_United Kingdom.1252 | English_United Kingdom.1252 | =c/postgres          +
           |          |          |                             |                             | postgres=CTc/postgres
 template1 | postgres | UTF8     | English_United Kingdom.1252 | English_United Kingdom.1252 | =c/postgres          +
           |          |          |                             |                             | postgres=CTc/postgres
(3 rows)


postgres-# \q

C:\WINDOWS\system32>psql -d analysis -U postgres
psql: FATAL:  database "analysis" does not exist

C:\WINDOWS\system32>

我不知道我在用 psql 做什么,但我发现我可以作为用户 postgres 连接到 db postgres。有一个\l命令,它显示了 3 个数据库(其中 2 个,template0 和 template1),我什至在 pgadmin 中都看不到。

如何连接到我刚刚在 pgadmin 中创建的数据库(分析)?谁能解释我在 pgadmin 中登录的用户,如果它与“postgres”不同,如果不是,我怎么在 psql 中看不到我在 pgadmin 中看到的内容?

标签: postgresql

解决方案


template0 和 template1 被称为骨架数据库。当您使用 CREATE DATABASE 命令 postgres 复制现有数据库。默认情况下,template1 数据库用于创建新数据库。

我认为您的 pgadmin 和 psql 连接到不同的集群。postgresql 中的集群是单个服务器实例中一个或多个数据库的集合

您发布的图像未显示 pgadmin 浏览器的完整视图。但是如果有 2 个集群,那么您可以在 pgadmin 中看到类似“Servers(2)”的信息。要查找每个集群的运行端口,请右键单击相应的集群并选择属性,然后单击连接选项卡。这里可以看到端口号。然后使用该端口连接到 psql 中的集群。


推荐阅读