-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.
9
您可以在使用psql连接时选择数据库 . 从脚本中使用它时很方便:
sudo -u postgres psql -c "CREATE SCHEMA test AUTHORIZATION test;" test
11 回答
使用psql的元命令
\c or \connect [ dbname [ username ] [ host ] [ port ] ] | conninfo
(参见documentation) .示例:
\c MyDatabase
请注意
\c
和\connect
元命令是 case-sensitive .如果要在启动时切换到特定数据库,请尝试
/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
中创建别名在命令行运行
psql
,它将切换到默认数据库;psql anotherdb
,它将在启动时切换到参数名称为db的数据库 .虽然在问题中没有明确说明,但目的是连接到特定的模式/数据库 .
另一种选择是直接连接到架构 . 例:
sudo -u postgres psql -d my_database_name
源自
man psql
:您可以在使用psql连接时选择数据库 . 从脚本中使用它时很方便:
您可以使用
\c <database>
或\connect <database>
连接到数据库 .使用下面的语句切换到驻留在postgreSQL RDMS中的不同数据库
\l
for databases\c
DatabaseName切换到db\df
,用于存储在特定数据库中的过程或简而言之
你可以使用连接
在PSQL提示符下,您可以执行以下操作:
如其他答案中所述,您需要更改连接以使用其他数据库 .
Postgres使用模式 . 您可以在单个数据库中拥有多个方案 . 因此,如果您在同一个数据库中工作,并且想要更改架构,则可以执行以下操作:
SET SCHEMA 'schema_name';