Wil*_*ung 1564
在PostgreSQL中,您可以使用\connect
客户端工具psql 的元命令:
\connect DBNAME
Run Code Online (Sandbox Code Playgroud)
或简而言之:
\c DBNAME
Run Code Online (Sandbox Code Playgroud)
Man*_*los 27
您可以在使用psql连接时选择数据库.从脚本中使用它时很方便:
sudo -u postgres psql -c "CREATE SCHEMA test AUTHORIZATION test;" test
Run Code Online (Sandbox Code Playgroud)
Jes*_*nan 27
你可以使用连接
\c dbname
Run Code Online (Sandbox Code Playgroud)
Ali*_*aka 11
虽然问题中没有明确说明,但目的是连接到特定的模式/数据库。
另一种选择是直接连接到架构。例子:
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.
Run Code Online (Sandbox Code Playgroud)
您可以使用以下方式连接
\c 数据库名
如果您想查看 POSTGRESQL 或 SQL 的所有可能命令,请按照以下步骤操作:
Rails dbconsole(您将被重定向到当前的 ENV 数据库)
?(对于 POSTGRESQL 命令)
或者
\h(对于 SQL 命令)
按 Q 退出
小智 7
列出和切换数据库在PostgreSQL\n当您需要在数据库之间进行更改时,\xe2\x80\x99 将使用 \\connect 命令,或 \\c 后跟数据库名称,如下所示:
\npostgres=# \\connect database_name\npostgres=# \\c database_name\n
Run Code Online (Sandbox Code Playgroud)\n检查您当前连接的数据库。
\nSELECT current_database();\n
Run Code Online (Sandbox Code Playgroud)\n\npostgres=# \\l\n postgres=# \\list\n
Run Code Online (Sandbox Code Playgroud)\n
如果您想在启动时切换到特定数据库,请尝试
/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
}
Run Code Online (Sandbox Code Playgroud)
命令行运行psql
,会切换到默认数据库;psql anotherdb
,它将在启动时切换到参数中名称的数据库。
您还可以连接到具有不同 ROLE 的数据库,如下所示。
\connect DBNAME ROLENAME;
Run Code Online (Sandbox Code Playgroud)
或者
\c DBNAME ROLENAME;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
612668 次 |
最近记录: |