Postgis安装:输入“geometry”不存在
我正在尝试使用 Postgis 创建表。我通过这个页面来完成此操作。但是当我导入 postgis.sql 文件时,我收到很多错误:
ERROR: type "geometry" does not exist
有人知道如何修复它吗?
I am trying to create table with Postgis. I do it by this page. But when I import postgis.sql file, I get a lot of errors:
ERROR: type "geometry" does not exist
Does anybody know how can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
我遇到了同样的问题,但通过运行以下代码解决了这个问题
详细信息,
I had the same problem, but it was fixed by running following code
In detail,
如果加载了 Postgis 扩展,则您的 SQL 可能找不到几何类型,因为缺少公共模式的搜索路径。
尝试
SET search_path = ..., public;
在脚本的第一行。 (将 ... 替换为其他所需的搜索路径)
If the Postgis-Extension is loaded, then your SQL perhaps does not find the geometry-type because of missing search-path to the public schema.
Try
SET search_path = ..., public;
in the first line of your scsript. (replace ... with the other required search-paths)
您可以从终端执行此操作:
You can do it from terminal:
如果您尝试在另一个架构而不是
public
上使用postgis类型,也可能会出现此错误。如果您使用 postgis 2.3 或更高版本创建自己的架构并遇到此错误,请执行以下操作 如此处所述:
然后您可以继续使用postgis功能。
This error may also occur if you try to use postgis types on another schema rather than
public
.If you are creating you own schema, using postgis 2.3 or higher and encounter this error, do the following as stated here:
Then you can proceed to use postgis functinalities.
要让 psql 在出现第一个错误时停止,请使用
-v ON_ERROR_STOP=1
(即 默认情况下关闭,这就是您看到许多错误的原因)。例如:实际错误类似于“无法加载库 X”,这可能会因您的情况而异。作为猜测,请在安装 sql 脚本之前尝试此命令:(
根据您的系统,您可能需要添加
sudo
前缀)。此命令更新所有系统库(例如 GEOS)的路径。To get psql to stop on the first error, use
-v ON_ERROR_STOP=1
(which is off by default, which is why you see many errors). For example:The actual error is something like "could not load library X", which can vary on your situation. As a guess, try this command before installing the sql script:
(you might need to prefix with
sudo
depending on your system). This command updates the paths to all system libraries, such as GEOS.您必须在数据库上启用该扩展。
psql my_database -c“创建扩展 postgis;”
You must enable the extension on your database.
psql my_database -c "CREATE EXTENSION postgis;"
您还需要确保您尝试使用 postgis 扩展的用户有权访问设置 postgis 的模式(在我读的教程中称为“postgis”)。
我刚刚遇到了这个错误,它已经解决了,因为我只授予了新用户对数据库的访问权限。在我创建的数据库中,我运行:
这解决了这个错误
You also need to ensure that the user you are trying to use the postgis extension as, has access to the schema where postgis is setup (which in the tutorials I read is called 'postgis').
I just had this error, and it was solved because I had only given a new user access to the database. In the database I'd created, I ran:
And this solved this error
这个问题已经得到了回答,但我想添加一个更彻底的答案来解释为什么某些命令有效,以及在什么情况下使用它们,当然还有如何确定您所处的情况。
首先,您需要检查 PostGIS 是否确实安装在您的盒子上。连接到 postgres 时(例如通过 psql),运行:
如果未安装,请查找发行版和版本特定的安装 PostGIS 的说明并安装它。
假设安装了 PostGIS,该错误通常是由于没有“创建”特定数据库的扩展(不幸的是,这是一种误导性的语言使用,其效果更像是“启用”扩展)而导致的。 PostgreSQL 的设置方式是,默认情况下新数据库不启用任何扩展,您需要为每个数据库启用(“创建”)它们。为此,您需要运行以下命令。
它只需要运行一次:
我认为您需要相关特定数据库的超级用户权限才能运行此命令。
假设 postgres 配置为允许权限,您可以通过运行以下命令从命令行执行此命令:
您可能需要使用
-U
标志并指定用户。然而,在某些情况下,扩展可能已经创建,并安装在与公共不同的架构下,问题可能是权限之一。如果您创建一个新用户但不授予其访问架构的权限,则可能会出现 @mozboz 描述的情况。要检测这种情况,请在数据库中查找单独的架构,其中包含一个名为
spatial_ref_sys
的表,因为该表是在创建扩展时创建的。在这种情况下,当连接到数据库时,您可能需要运行:
根据我的经验,这种情况很少见,而且我从未找到任何理由以这种方式进行设置。
schema_name
经常是,但并不总是postgis
。默认情况下,如果您在此处运行第一个命令,它将在public
架构下创建扩展。我认为 USAGE 对于大多数情况来说通常就足够了,但是如果您希望用户能够实际编辑数据,您可能需要授予更多权限;我唯一一次遇到的情况是向
spatial_ref_system
添加新的投影,但即使这种情况也很少见,因为默认情况下该表包含最常用的投影。This has already been answered but I wanted to add a more thorough answer that explains why certain commands work, and in what circumstances to use them, and of course, how to figure out which circumstances you are in.
First, you need to check that PostGIS is actually installed on your box. When connected to postgres, such as via psql, run:
If it's not installed, look up distro- and version-specific instructions for installing PostGIS and install it.
Assuming PostGIS is installed, the error is usually the result of not having "created" (this is an unfortunately misleading use of language, the effect is more like "enabling" the extension) the extension for the particular database. The way PostgreSQL is set up, by default new databases do not come with any extensions enabled, and you need to enable ("create") them per-database. In order to do this you need to run the following command.
It only needs to be run once:
I think you need superuser privileges for the particular database in question, in order to run this command.
Assuming postgres is configured so that the permissions allow, you can execute this command from the command line by running the following command:
You may need to use the
-U
flag and specify a user.In some cases, however, the extension may have already been created, and installed under a different schema than public, and the problem may be one of permissions. This can arise like in the situation @mozboz describes, if you create a new user but don't give it access to the schema. To detect this case, look for a separate schema in the database, with a table called
spatial_ref_sys
, as this is created when the extension is created.In this case you may need to run, when connected to the database:
In my experience, this situation is rare, and I have never found any reason to set things up this way. The
schema_name
is often, but not alwayspostgis
. By default if you run the first command here, it will create the extension under thepublic
schema.I think USAGE is usually sufficient for most cases, but you might want to grant more privileges if you want the user to be able to actually edit data; the only time this has ever come up for me was adding new projections to
spatial_ref_system
, but even this is rare as by default that table includes most commonly used projections.使用 pgAdmin 4,您可以对此进行排序:
单击 SQL 查询按钮(或转到“工具”>“SQL 查询”)。
查询工具)。
在查询文本字段中输入以下查询以加载 PostGIS 空间扩展,然后单击工具栏中的“播放”按钮(或按 F5)以“执行查询”。
成功反馈
现在通过运行 PostGIS 函数确认 PostGIS 已安装:
代码
反馈
Using pgAdmin 4,you can sort this:
Click on the SQL query button (or go to Tools >
Query Tool).
Enter the following query into the query text field to load the PostGIS spatial extension and Click the Play button in the toolbar (or press F5) to “Execute the query.”
Succesful feedback
Now confirm that PostGIS is installed by running a PostGIS function:
Code
Feedback
这里的答案可能会解决您的问题,但是如果您已经在数据库上启用了 postgis,则问题可能是您正在尝试将 postgis 表(带有几何列)恢复到启用 postgis 扩展的架构之外的架构中。在 pgAdmin 中,您可以单击 postgis 扩展并查看指定的模式。如果您尝试将具有几何列的表恢复到不同的架构中,则可能会收到此错误。
我通过更改 postgis 扩展解决了这个问题 - 但我不确定这是否一定是最好的方法。我所知道的是它让我恢复了桌子。
The answers here may solve your problem, however if you already have postgis enabled on your DB, the issue may be that you are trying to restore a postgis table (with a geometry column) into a schema other than where your postgis extension is enabled. In pgAdmin you can click on the postgis extension and see which schema is specified. If you are trying to restore a table with geometry column into a different schema, you might get this error.
I resolved this by altering my postgis extension - however I'm not sure if that was necessarily the best way to do it. All I know is that it allowed me to restore the table.
首先确保您已安装(与 pg 版本匹配:psql -V)postgis:
在创建表之前添加:
First make sure you have (matching to pg version: psql -V) postgis installed:
Just before tables creation add:
验证用户的
public
search_path
是否包含:方法#1:
SET
将search_path
更改为public
方法 #2: 使用
GEOMETRY 的限定对象名称代码> 对象类型 (
public.GEOMETRY
)资料来源: 揭秘模式和模式通过示例搜索路径
Verify the
public
search_path
is not included for the user:Method #1:
SET
thesearch_path
topublic
Method #2: Use the qualified object name for the
GEOMETRY
object type (public.GEOMETRY
)Source: Demystifying Schemas & search_path through Examples
或者...
在你的 python 程序中,使用 psycopg2 中的当前光标。
Or...
in your python program, using a current cursor from psycopg2.
我的经验是我试图将 ism 文件加载到与“public”不同的文件中。我在 public 之外的另一个模式上初始化了 postgis 扩展。所以那不起作用。所以最后我不得不删除扩展,然后公开创建 postgis 扩展。之后我可以将 osm 加载到我的新模式中
Osm 总是在公共场合查找扩展文件,无论您打算在另一个模式中安装 osm 文件的位置
My experience was I was trying to load the ism file to a different file than "public". I initialised the postgis extension on another schema other than public. So that didn't work. So finally I had to remove the extension, and than created the postgis extension in public. After that I could load the osm into my new schema
Osm always looks for the extension file in public, irregardless of where u intend to install the osm files in another schema