如何在psql中切换数据库?
在 MySQL 中,我使用了 use database_name;
等效的 psql
是什么?
In MySQL, I used use database_name;
What's the psql
equivalent?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在 MySQL 中,我使用了 use database_name;
等效的 psql
是什么?
In MySQL, I used use database_name;
What's the psql
equivalent?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(16)
在 PostgreSQL 中,您可以使用
\ connect
客户端工具psql的元命令:或简称:
In PostgreSQL, you can use the
\connect
meta-command of the client tool psql:or in short:
您可以使用
\c
或\connect
连接到数据库。You can connect to a database with
\c <database>
or\connect <database>
.在 PSQL 提示符下,您可以执行以下操作:
At the PSQL prompt, you can do:
使用
\c databaseName
或\connect databaseName
(使用 psql 13.3)
use
\c databaseName
or\connect databaseName
(Working on psql 13.3)
使用psql连接时可以选择数据库。从脚本中使用它时这很方便:
You can select the database when connecting with psql. This is handy when using it from a script:
尽管问题中没有明确说明,但目的是连接到特定的架构/数据库。
另一种选择是直接连接到架构。示例:
sudo -u postgres psql -d my_database_name
来源自
man psql
:Though not explicitly stated in the question, the purpose is to connect to a specific schema/database.
Another option is to directly connect to the schema. Example:
sudo -u postgres psql -d my_database_name
Source from
man psql
:\l
用于数据库\c
DatabaseName 切换到 db\df
用于存储在特定数据库中的过程\l
for databases\c
DatabaseName to switch to db\df
for procedures stored in particular database使用 psql 的元命令
\c 或 \connect [ dbname [ username ] [ host ] [ port ] ] | conninfo
(请参阅文档)。示例:
\c MyDatabase
请注意,
\c
和\connect
元命令是区分大小写。Using psql's meta-command
\c or \connect [ dbname [ username ] [ host ] [ port ] ] | conninfo
(see documentation).Example:
\c MyDatabase
Note that the
\c
and\connect
meta-commands are case-sensitive.使用下面的语句切换到驻留在其中的不同数据库
你的 postgreSQL RDMS
Use below statement to switch to different databases residing inside
your postgreSQL RDMS
您还可以使用不同的角色连接到数据库,如下所示。
或者
You can also connect to a database with a different ROLE as follows.
or
您可以使用
\c dbname
连接如果您想查看 POSTGRESQL 或 SQL 的所有可能命令,请按照以下步骤操作:
rails dbconsole
(您将被重定向到当前的 ENV 数据库)
?
(对于 POSTGRESQL 命令)
或
\h
(对于 SQL 命令)
按 Q 退出
You can connect using
\c dbname
If you would like to see all possible commands for POSTGRESQL or SQL follow this steps :
rails dbconsole
(You will be redirected to your current ENV database)
?
(For POSTGRESQL commands)
or
\h
(For SQL commands)
Press Q to Exit
在 PostgreSQL 中列出和切换数据库
当您需要在数据库之间进行更改时,您将使用 \connect 命令,或 \c 后跟数据库名称,如下所示:
检查当前连接到的数据库。
PostgreSQL 列出数据库
Listing and Switching Databases in PostgreSQL
When you need to change between databases, you’ll use the \connect command, or \c followed by the database name as shown below:
Check the database you are currently connected to.
PostgreSQL List Databases
如果您想在启动时切换到特定数据库,请尝试
/Applications/Postgres.app/Contents/Versions/9.5/bin/psql vigneshdb;
默认情况下,Postgres 在端口 5432 上运行。在另一个上运行,请确保在命令行中传递端口。
/Applications/Postgres.app/Contents/Versions/9.5/bin/psql -p2345 vigneshdb;
通过一个简单的别名,我们可以让它变得方便。
在
.bashrc
或.bash_profile
中创建别名在命令行中运行
psql
,它将切换到默认数据库;psql anotherdb
,它将在启动时切换到参数中名称的数据库。If you want to switch to a specific database on startup, try
/Applications/Postgres.app/Contents/Versions/9.5/bin/psql vigneshdb;
By default, Postgres runs on the port 5432. If it runs on another, make sure to pass the port in the command line.
/Applications/Postgres.app/Contents/Versions/9.5/bin/psql -p2345 vigneshdb;
By a simple alias, we can make it handy.
Create an alias in your
.bashrc
or.bash_profile
Run
psql
in command line, it will switch to default database;psql anotherdb
, it will switch to the db with the name in argument, on startup.“使用数据库名称;”的正确翻译从 mysql 到 postgreSQL 必须考虑到在 postgreSQL 中你也有模式。通常您的连接字符串已经定义了数据库名称,您必须指定默认架构。
设置模式名称后,您可以执行如下查询:
而无需重复 schemaName,如下所示:
The correct translation of 'use databasename;' from mysql to postgreSQL has to consider that in postgreSQL you have also schemas. Usually your connection string has alreday defined the databasename, you have to specify the default schema.
After setting the schema name you can execute query like:
without repeating the schemaName like here:
您只需输入
use [dbName]
即可在数据库之间切换,而无需重新输入密码。You can just enter
use [dbName]
to switch between databases without reentering your password.