您可以使用\c <database>
或\connect <database>
。
在 PSQL 提示符下,您可以执行以下操作:
\connect (or \c) dbname
您可以使用连接
\c dbname
与 psql 连接时,可以选择数据库。从脚本中使用它很方便:
sudo -u postgres psql -c "CREATE SCHEMA test AUTHORIZATION test;" test
\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
,它将在启动时切换到参数为 name 的数据库。
尽管未在问题中明确说明,但目的是连接到特定的架构 / 数据库。
另一个选择是直接连接到架构。例:
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.