博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PostgreSQL中的“use database_name”命令
阅读量:3580 次
发布时间:2019-05-20

本文共 2833 字,大约阅读时间需要 9 分钟。

本文翻译自:

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 . 我通过搜索Internet找到了\\c databasename ,但它只在上运行。 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楼

参考:


#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 

转载地址:http://zmlgj.baihongyu.com/

你可能感兴趣的文章
linux VM虚拟机可以ping通主机,但主机无法ping通虚拟机
查看>>
C++ 中Struct与typedef struct总结
查看>>
WNetAddConnection2调用失败,错误码1200/1312
查看>>
POI读写Excel的基本使用
查看>>
淘宝网站的架构演进
查看>>
设置zookeeper开机自启动流程
查看>>
CentOS安装mysql5.7的教详细流程
查看>>
项目整合微信扫码登录功能
查看>>
分布式文件系统FastDfs的搭建
查看>>
Springboot项目利用Java客户端调用FastDFS
查看>>
全文检索工具elasticsearch的安装和简单介绍
查看>>
利用Kibana学习全文检索工具elasticsearch
查看>>
SpringBoot在Test测试类或自定义类中通过@Autowired注入为null
查看>>
使用docker搭建YAPI服务
查看>>
西南科技大学OJ题 邻接表到邻接矩阵1056
查看>>
西南科技大学OJ题 有向图的出度计算1057
查看>>
西南科技大学OJ题 有向图的最大出度计算1059
查看>>
西南科技大学OJ题 带权有向图计算1063
查看>>
oracle主键自增触发器编写
查看>>
String与StringBuilder与StringBuffer三者的差别
查看>>