本文翻译自:“use database_name” command in PostgreSQL
I am beginner to PostgreSQL. 我是PostgreSQL的初学者。
I want to connect to another database from the query editor of Postgres - like the USE
command of MySQL or MS SQL Server. 我想从Postgres的查询编辑器连接到另一个数据库 - 比如MySQL或MS SQL Server的USE
命令。
I found \\c databasename
by searching the Internet, but its runs only on psql . 我通过搜索Internet找到了\\c databasename
,但它只在psql上运行。 When I try it from the PostgreSQL query editor I get a syntax error. 当我从PostgreSQL查询编辑器中尝试它时,我收到语法错误。
I have to change the database by pgscripting. 我必须通过pgscripting更改数据库。 Does anyone know how to do it? 有谁知道怎么做?
#1楼
参考:https://stackoom.com/question/hMkb/PostgreSQL中的-use-database-name-命令
#2楼
When you get a connection to PostgreSQL
it is always to a particular database. 当你获得与PostgreSQL
的连接时,它始终是一个特定的数据库。 To access a different database, you must get a new connection. 要访问其他数据库,您必须获得新连接。
Using \\c
in psql closes the old connection and acquires a new one, using the specified database and/or credentials. 在psql中使用\\c
使用指定的数据库和/或凭据关闭旧连接并获取新连接。 You get a whole new back-end process and everything. 你会得到一个全新的后端流程和一切。
#3楼
You must specify the database to use on connect; 您必须指定要在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. 干杯,刚刚一直忙着寻找这个,关于postgreSQL的信息与MySQL相比太少,其余在我看来。
#4楼
In pgAdmin you can also use 在pgAdmin中你也可以使用
SET search_path TO your_db_name; SET search_path TO your_db_name;
#5楼
The basic problem while migrating from MySQL I faced was, I thought of the term database
to be same in PostgreSQL also, but it is not. 从我面临的MySQL迁移时的基本问题是,我认为术语database
在PostgreSQL中也是相同的,但事实并非如此。 So if we are going to switch the database from our application or pgAdmin
, the result would not be as expected. 因此,如果我们要从我们的应用程序或pgAdmin
切换数据库,结果将不会如预期的那样。 As in my case, we have separate schemas (Considering PostgreSQL terminology here.) for each customer and separate admin schema. 在我的例子中,我们为每个客户和单独的管理模式提供了单独的模式(在这里考虑PostgreSQL术语。)。 So in application, I have to switch between schemas. 所以在应用程序中,我必须在模式之间切换。
For this, we can use the SET search_path
command. 为此,我们可以使用SET search_path
命令。 This does switch the current schema to the specified schema name for the current session. 这会将当前架构切换为当前会话的指定架构名称。
example: 例:
SET search_path = different_schema_name;
This changes the current_schema to the specified schema for the session. 这会将current_schema更改为会话的指定架构。 To change it permanently, we have to make changes in postgresql.conf
file. 要永久更改它,我们必须在postgresql.conf
文件中进行更改。
#6楼
首次连接到psql
时使用此commad
=# psql <databaseName> <usernamePostgresql>