PostgreSQL检查表是否存在并抛出“关系不存在”。错误 [英] PostgreSQL check if table exist throwing "relation does not exist" error
本文介绍了PostgreSQL检查表是否存在并抛出“关系不存在”。错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PostgreSQL中,我尝试检查表是否存在:
In PostgreSQL I try to check if table exist:
SELECT EXISTS (SELECT * FROM table_name);
这会引发错误。我如何检查表是否已经存在,所以结果将是布尔值?因为目前我可以使用try-catch(如果不存在则输入)来实现相同的结果,而不是对结果进行if-else ...
And it throwing an error. How can I check if table already exists so the result will be boolean? Because currently I can achieve the same with try-catch (enters to catch if not exist) instead of if-else on the result...
谢谢,
推荐答案
尽管根据权限的设置方式,您可能都无法访问表,但这两种方法都应该有效:
Either of these should work, though depending on how your permissions are set up you may not have access to the tables:
选择存在(从pg_class中选择relname,其中relname ='table_name');
选择存在(从information_schema.tables中选择table_name ='table_name');
这篇关于PostgreSQL检查表是否存在并抛出“关系不存在”。错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文