在mysql中我用过 use database_name;

PSQL等效的是什么?

有帮助吗?

解决方案

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

\connect DBNAME

或简而言之:

\c DBNAME

其他提示

您可以使用 \c <database> 或者 \connect <database>.

在PSQL提示下,您可以做:

\connect (or \c) dbname

您可以使用

\c dbname

与PSQL连接时,您可以选择数据库。从脚本使用它时,这很方便:

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

使用PSQL的元命令 \c or \connect [ dbname [ username ] [ host ] [ port ] ] | conninfo (看 文档).

例子: \c MyDatabase

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

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

使用以下语句切换到驻留于PostgreSQL RDMS内部的不同数据库

\c databaseName

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

/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, ,它将在启动时切换到参数中的名称。

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

另一个选择是直接连接到模式。例子:

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.

您可以使用

c dbname

如果您想查看PostgreSQL或SQL的所有可能命令,请执行以下步骤:

  1. Rails dbConsole(您将重新置于当前的ENV数据库)

  2. ? (对于PostgreSQL命令)

或者

  1. h(用于SQL命令)

  2. 按Q退出

您也可以连接到具有不同角色的数据库。

\connect DBNAME ROLENAME;

或者

\c DBNAME ROLENAME;

如其他答案中所述,您需要更改连接以使用其他数据库。

Postgres与模式合作。您可以在单个数据库中使用多个方案。因此,如果您在同一数据库中工作并想更改模式,则可以执行:

SET SCHEMA 'schema_name';

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top