PostgreSQL中的“use database_name”命令

本文介绍了在PostgreSQL中如何从查询编辑器连接到不同的数据库。与MySQL或MS SQL Server不同,`USE`命令在PostgreSQL的psql中工作,会关闭旧连接并打开新连接。如果在pgAdmin中,可以使用`SET search_path TO your_db_name;`来切换数据库。对于应用或查询编辑器,可能需要在会话中切换模式,这可以通过`SET current_schema TO your_schema_name;`命令完成。
摘要由CSDN通过智能技术生成

本文翻译自:“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>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值