PostgreSQL 中的“使用数据库名称”命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10335561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:28:23  来源:igfitidea点击:

"use database_name" command in PostgreSQL

postgresqlpostgresql-9.1

提问by sam

I am beginner to PostgreSQL.

我是 PostgreSQL 的初学者。

I want to connect to another database from the query editor of Postgres - like the USEcommand of MySQL or MS SQL Server.

我想从 Postgres 的查询编辑器连接到另一个数据库 - 比如USEMySQL 或 MS SQL Server的命令。

I found \c databasenameby searching the Internet, but its runs only on psql. When I try it from the PostgreSQL query editor I get a syntax error.

\c databasename通过搜索互联网发现,但它仅在psql上运行。当我从 PostgreSQL 查询编辑器尝试它时,我收到一个语法错误。

I have to change the database by pgscripting. Does anyone know how to do it?

我必须通过 pgscripting 更改数据库。有谁知道怎么做?

回答by kgrittn

When you get a connection to PostgreSQLit is always to a particular database. To access a different database, you must get a new connection.

当您获得与PostgreSQL它的连接时,它总是与特定数据库的连接。要访问不同的数据库,您必须获得一个新连接。

Using \cin psql closes the old connection and acquires a new one, using the specified database and/or credentials. You get a whole new back-end process and everything.

\c在 psql 中使用会关闭旧连接并使用指定的数据库和/或凭据获取新连接。你会得到一个全新的后端流程和一切。

回答by Eugene

You must specify the database to use on connect; if you want to use psql for your script, you can use "\c name_database"

您必须指定要在连接时使用的数据库;如果你想在你的脚本中使用 psql,你可以使用 "\c name_database"

user_name=# CREATE DATABASE testdatabase; 
user_name=# \c testdatabase 

At this point you might see the following output

此时你可能会看到以下输出

You are now connected to database "testdatabase" as user "user_name".
testdatabase=#

Notice how the prompt changes. Cheers, have just been hustling looking for this too, too little information on postgreSQL compared to MySQL and the rest in my view.

注意提示是如何变化的。干杯,也一直在忙着寻找这个,在我看来,与 MySQL 相比,关于 postgreSQL 的信息太少了。

回答by Bart De Boeck

In pgAdmin you can also use

在 pgAdmin 中,您还可以使用

SET search_path TO your_db_name;

将 search_path 设置为 your_db_name;

回答by VPK

The basic problem while migrating from MySQL I faced was, I thought of the term databaseto be same in PostgreSQL also, but it is not. So if we are going to switch the database from our application or pgAdmin, the result would not be as expected. As in my case, we have separate schemas (Considering PostgreSQL terminology here.) for each customer and separate admin schema. So in application, I have to switch between schemas.

从 MySQL 迁移时我面临的基本问题是,我认为该术语database在 PostgreSQL 中也相同,但事实并非如此。因此,如果我们要从我们的应用程序或 切换数据库pgAdmin,结果将不会像预期的那样。与我的情况一样,我们为每个客户和单独的管理模式提供了单独的模式(在此考虑 PostgreSQL 术语。)。所以在应用程序中,我必须在模式之间切换。

For this, we can use the SET search_pathcommand. This does switch the current schema to the specified schema name for the current session.

为此,我们可以使用该SET search_path命令。这确实将当前模式切换到当前会话的指定模式名称。

example:

例子:

SET search_path = different_schema_name;

This changes the current_schema to the specified schema for the session. To change it permanently, we have to make changes in postgresql.conffile.

这会将 current_schema 更改为会话的指定模式。要永久更改它,我们必须在postgresql.conf文件中进行更改。

回答by Sukma Saputra

Use this commad when first connect to psql

首次连接时使用此命令 psql

=# psql <databaseName> <usernamePostgresql>