如何在psql中切换数据库?

发布于 2024-09-27 22:25:04 字数 87 浏览 2 评论 0原文

在 MySQL 中,我使用了 use database_name;

等效的 psql 是什么?

In MySQL, I used use database_name;

What's the psql equivalent?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

久伴你 2024-10-04 22:25:04

在 PostgreSQL 中,您可以使用 \ connect 客户端工具psql的元命令:

\connect DBNAME

或简称:

\c DBNAME

In PostgreSQL, you can use the \connect meta-command of the client tool psql:

\connect DBNAME

or in short:

\c DBNAME
黑寡妇 2024-10-04 22:25:04

您可以使用 \c\connect 连接到数据库。

You can connect to a database with \c <database> or \connect <database>.

給妳壹絲溫柔 2024-10-04 22:25:04

在 PSQL 提示符下,您可以执行以下操作:

\connect (or \c) dbname

At the PSQL prompt, you can do:

\connect (or \c) dbname
吹梦到西洲 2024-10-04 22:25:04

使用 \c databaseName\connect databaseName

(使用 psql 13.3)

use \c databaseName or \connect databaseName

(Working on psql 13.3)

醉态萌生 2024-10-04 22:25:04

使用psql连接时可以选择数据库。从脚本中使用它时这很方便:

sudo -u postgres psql -c "CREATE SCHEMA test AUTHORIZATION test;" test

You can select the database when connecting with psql. This is handy when using it from a script:

sudo -u postgres psql -c "CREATE SCHEMA test AUTHORIZATION test;" test
南汐寒笙箫 2024-10-04 22:25:04

尽管问题中没有明确说明,但目的是连接到特定的架构/数据库。

另一种选择是直接连接到架构。示例:

sudo -u postgres psql -d my_database_name

来源自 man psql

-d dbname
--dbname=dbname
   Specifies the name of the database to connect to. This is equivalent to specifying dbname as the first non-option argument on the command line.

   If this parameter contains an = sign or starts with a valid URI prefix (postgresql:// or postgres://), it is treated as a conninfo string. See Section 31.1.1, “Connection Strings”, in the
   documentation for more information.

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:

-d dbname
--dbname=dbname
   Specifies the name of the database to connect to. This is equivalent to specifying dbname as the first non-option argument on the command line.

   If this parameter contains an = sign or starts with a valid URI prefix (postgresql:// or postgres://), it is treated as a conninfo string. See Section 31.1.1, “Connection Strings”, in the
   documentation for more information.
早茶月光 2024-10-04 22:25:04

\l 用于数据库
\c DatabaseName 切换到 db
\df 用于存储在特定数据库中的过程

\l for databases
\c DatabaseName to switch to db
\df for procedures stored in particular database

沩ん囻菔务 2024-10-04 22:25:04

使用 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.

凡尘雨 2024-10-04 22:25:04

使用下面的语句切换到驻留在其中的不同数据库
你的 postgreSQL RDMS

\c databaseName

Use below statement to switch to different databases residing inside
your postgreSQL RDMS

\c databaseName
空城旧梦 2024-10-04 22:25:04

您还可以使用不同的角色连接到数据库,如下所示。

\connect DBNAME ROLENAME;

或者

\c DBNAME ROLENAME;

You can also connect to a database with a different ROLE as follows.

\connect DBNAME ROLENAME;

or

\c DBNAME ROLENAME;
迷途知返 2024-10-04 22:25:04

您可以使用

\c dbname

连接如果您想查看 POSTGRESQL 或 SQL 的所有可能命令,请按照以下步骤操作:

  1. rails dbconsole
    (您将被重定向到当前的 ENV 数据库)

  2. ?
    (对于 POSTGRESQL 命令)

  1. \h
    (对于 SQL 命令)

  2. 按 Q 退出

You can connect using

\c dbname

If you would like to see all possible commands for POSTGRESQL or SQL follow this steps :

  1. rails dbconsole
    (You will be redirected to your current ENV database)

  2. ?
    (For POSTGRESQL commands)

or

  1. \h
    (For SQL commands)

  2. Press Q to Exit

永言不败 2024-10-04 22:25:04

PostgreSQL 中列出和切换数据库
当您需要在数据库之间进行更改时,您将使用 \connect 命令,或 \c 后跟数据库名称,如下所示:

postgres=# \connect database_name
postgres=# \c database_name

检查当前连接到的数据库。

SELECT current_database();

PostgreSQL 列出数据库

postgres=# \l
 postgres=# \list

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:

postgres=# \connect database_name
postgres=# \c database_name

Check the database you are currently connected to.

SELECT current_database();

PostgreSQL List Databases

postgres=# \l
 postgres=# \list
放手` 2024-10-04 22:25:04

如果您想在启动时切换到特定数据库,请尝试

/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中创建别名

function psql()
{
    db=vigneshdb
    if [ "$1" != ""]; then
            db=$1
    fi
    /Applications/Postgres.app/Contents/Versions/9.5/bin/psql -p5432 $1
}

在命令行中运行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

function psql()
{
    db=vigneshdb
    if [ "$1" != ""]; then
            db=$1
    fi
    /Applications/Postgres.app/Contents/Versions/9.5/bin/psql -p5432 $1
}

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.

生死何惧 2024-10-04 22:25:04
  Connect to database:

  Method 1 : enter to db : sudo -u postgres psql

  Connect to db : \c dbname

  Method 2 : directly connect to db : sudo -u postgres psql -d my_database_name
  Connect to database:

  Method 1 : enter to db : sudo -u postgres psql

  Connect to db : \c dbname

  Method 2 : directly connect to db : sudo -u postgres psql -d my_database_name
逆蝶 2024-10-04 22:25:04
SET schema 'schemaName';

“使用数据库名称;”的正确翻译从 mysql 到 postgreSQL 必须考虑到在 postgreSQL 中你也有模式。通常您的连接字符串已经定义了数据库名称,您必须指定默认架构。

设置模式名称后,您可以执行如下查询:

SELECT * from tableName;

而无需重复 schemaName,如下所示:

SELECT * from schemaName.tableName;
SET schema '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:

SELECT * from tableName;

without repeating the schemaName like here:

SELECT * from schemaName.tableName;
时光沙漏 2024-10-04 22:25:04

您只需输入use [dbName]即可在数据库之间切换,而无需重新输入密码。

You can just enter use [dbName] to switch between databases without reentering your password.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文