postgresql 如何在psql中切换数据库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3949876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 22:41:38  来源:igfitidea点击:

How to switch databases in psql?

postgresqlpsql

提问by Blankman

In MySQL, I used use database_name;

MySQL 中,我使用use database_name;

What's the psqlequivalent?

什么是psql等价物?

回答by Will Hartung

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

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

\connect DBNAME

or in short:

或者简而言之:

\c DBNAME

回答by meagar

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

您可以使用\c <database>或连接到数据库\connect <database>

回答by Michael Goldshteyn

At the PSQL prompt, you can do:

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

\connect (or \c) dbname

回答by Manel Clos

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

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

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

回答by Ambrish Rajput

\lfor databases \cDatabaseName to switch to db \dffor procedures stored in particular database

\l对于数据库 \cDatabaseName 切换到 db \df以获取存储在特定数据库中的过程

回答by Franck Dernoncourt

Using psql's meta-command \c or \connect [ dbname [ username ] [ host ] [ port ] ] | conninfo(see documentation).

使用 psql 的元命令 \c or \connect [ dbname [ username ] [ host ] [ port ] ] | conninfo(参见文档)。

Example: \c MyDatabase

例子: \c MyDatabase

Note that the \cand \connectmeta-commands are case-sensitive.

请注意,\c\connect元命令区分大小写

回答by Bilal Mahmood

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

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

\c databaseName

回答by Vignesh Raja

If you want to switch to a specific database on startup, try

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

/Applications/Postgres.app/Contents/Versions/9.5/bin/psql vigneshdb;

/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.

默认情况下,Postgres 在 5432 端口上运行。如果它在另一个上运行,请确保在命令行中传递该端口。

/Applications/Postgres.app/Contents/Versions/9.5/bin/psql -p2345 vigneshdb;

/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 .bashrcor .bash_profile

在您的.bashrc.bash_profile

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

Run psqlin command line, it will switch to default database; psql anotherdb, it will switch to the db with the name in argument, on startup.

psql在命令行运行,会切换到默认数据库;psql anotherdb,它将在启动时切换到名称为参数的数据库。

回答by AlikElzin-kilaka

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

sudo -u postgres psql -d my_database_name

Source from man psql:

来源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.

回答by Abhishek

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

您还可以连接到具有不同 ROLE 的数据库,如下所示。

\connect DBNAME ROLENAME;

or

或者

\c DBNAME ROLENAME;