如何在postgres中切换数据库? [英] How to switch databases in postgres?
问题描述
在SQL Server中,我使用:
In SQL Server, I use:
use database_name
Go
Select * from table_name
在postgreSQL中,我仍然使用:
In postgreSQL, I still use:
\connect database_name
Select * from table_name
========================
then error:
ERROR: syntax error at or near "\"
LINE 1: \connect database_name
^
********** Error **********
ERROR: syntax error at or near "\"
SQL state: 42601
Character: 1.
为什么?
能帮到我吗?谢谢。
Why? Can do help me? Thanks.
推荐答案
从技术上讲,PostgreSQL无法切换数据库。您必须断开连接并重新连接到新数据库。
Technically PostgreSQL can't switch databases. You must disconnect and reconnect to the new DB.
psql
命令行客户端将为您完成此操作 \connect
命令,快捷方式 \c
。但是这些不是PostgreSQL服务器处理的命令,它们是客户端命令。不同的客户不会理解或支持他们。
The psql
command-line client will do this for you with the \connect
command, shortcut \c
. But these are not commands processed by the PostgreSQL server, they're client commands. Different clients won't understand or support them.
猜测您使用的是PgAdmin-III,在这种情况下使用查询工具中的下拉菜单切换数据库。
At a guess you're using PgAdmin-III, in which case use the pulldown menu in the query tool to switch databases.
有一天我想要将 psql
的反斜杠命令代码提取到一个库中,PgAdmin之类的东西也可以链接并使用。
Some day I'd like to extract psql
's backslash-commands code into a library that things like PgAdmin could link to and use too.
这篇关于如何在postgres中切换数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!