【问题标题】:How can I change database inside Shell on Google Cloud Platform using PostgreSQL (psql)?如何使用 PostgreSQL (psql) 在 Google Cloud Platform 上的 Shell 内更改数据库?
【发布时间】:2018-04-27 17:20:01
【问题描述】:

在 GCP 上,当您连接到 Google Cloud Shell,然后使用“gcloud sql connect...”连接到 PostgreSQL 数据库实例时,GCP 会将您连接到 postgres 数据库。

如何更改到另一个已经创建的数据库?

是否有任何 psql(或特定的 GCP Shell)命令来更改数据库连接?

【问题讨论】:

    标签: database postgresql shell google-cloud-platform database-connection


    【解决方案1】:

    我没有尝试过 GCP,所以我不确定这是否适用。但在我的一些脚本中,我需要这样做。

    在这些脚本中,我使用

    \c 数据库名称

    更多信息:How to switch databases in psql?

    【讨论】:

    • 但是我应该在哪里输入呢?
    • 如果你通过 psql 运行它,你可以把它直接放在你的脚本中。这是我创建的一个要点,它展示了如何做到这一点。 gist.github.com/ssargent/e315cd90ebd9ad9b915f324be71cc41e 只需添加 \c DBNAME;在脚本中,它会更改数据库。在这个例子中,我连接到我的主数据库,然后切换到我创建的两个数据库 db1 和 db2
    【解决方案2】:

    正如@scottsargent 的回答:\c DBNAME 是解决方案。

    您需要在 GCP Shell 末尾添加分号 ;,按 ENTER 并在 psql 请求时提供用户密码以更改数据库连接:

    postgres=> \c elboticario;
    Password for user postgres:
    psql (9.6.7, server 9.6.6)
    SSL connection (protocol: TLSv1.2, cipher: **************-SHA256, bits: 128, compression: off)
    You are now connected to database "elboticario" as user "postgres".
    elboticario=>
    

    【讨论】:

      猜你喜欢
      • 2021-10-21
      • 1970-01-01
      • 2020-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-21
      • 1970-01-01
      相关资源
      最近更新 更多