在MySQL中,我使用use database_name;

psql的等价是什么?


当前回答

在PostgreSQL中列出和切换数据库 当您需要在数据库之间进行更改时,您将使用\connect命令,或\c后面跟着数据库名称,如下所示:

postgres=# \connect database_name
postgres=# \c database_name

检查当前连接的数据库。

SELECT current_database();

PostgreSQL列表数据库

postgres=# \l
 postgres=# \list

其他回答

  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

您还可以使用不同的ROLE连接到数据库,如下所示。

\connect DBNAME ROLENAME;

or

\c DBNAME ROLENAME;

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

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

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数据库名或\connect数据库名

(正在使用psql 13.3)

在PostgreSQL中列出和切换数据库 当您需要在数据库之间进行更改时,您将使用\connect命令,或\c后面跟着数据库名称,如下所示:

postgres=# \connect database_name
postgres=# \c database_name

检查当前连接的数据库。

SELECT current_database();

PostgreSQL列表数据库

postgres=# \l
 postgres=# \list