在MySQL中,我使用use database_name;
psql的等价是什么?
在MySQL中,我使用use database_name;
psql的等价是什么?
当前回答
如果您想在启动时切换到特定的数据库,请尝试
/应用程序/ Postgres.app /内容/版本/ 9.5 / bin / psql vigneshdb;
默认情况下,Postgres运行在端口5432上。如果它运行在另一个端口上,请确保在命令行中传递该端口。
/Applications/ postgress .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,它将在启动时切换到参数中有名称的db。
其他回答
虽然问题中没有明确说明,但目的是连接到特定的模式/数据库。
另一种选择是直接连接到模式。例子:
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 <database>或\connect <database>连接到数据库。
在PSQL提示符下,你可以这样做:
\connect (or \c) dbname
使用下面的语句切换到内部的不同数据库 您的postgreSQL RDMS
\c databaseName
Connect to database:
Method 1 : enter to db : sudo -u postgres psql
Connect to db : \c dbname
Method 2 : directly connect to db : sudo -u postgres psql -d my_database_name