简体   繁体   English

PostgreSQL的发行,安装和连接

[英]Distribution, Installation, and Connectivity of PostgreSQL

Have not been able to find an answer to this yet, but if this a duplicate, please mark accordingly! 尚未找到答案,但是如果重复,请相应标记!

Trying to understand how PostgreSQL can be distributed to an end-user via silent installation and the respective actions if: 尝试了解如何通过静默安装将PostgreSQL分发给最终用户以及在以下情况下的相应操作:

  1. PostgreSQL is already installed on the computer system PostgreSQL已安装在计算机系统上
  2. How to connect to PostgreSQL as a superuser, if it is already installed 如果已经安装了如何以超级用户身份连接到PostgreSQL
  3. Create a database (relating to #2, since we would not know the credentials of postgresql user when PostgreSQL was initially installed) 创建一个数据库(与#2有关,因为最初安装PostgreSQL时我们不知道postgresql用户的凭证)

For #1, from my research and understanding, there are two methods: 对于#1,根据我的研究和理解,有两种方法:

  1. Determining if a postgresql-[version] service is installed (per this QA ) 确定是否安装了postgresql- [version]服务(根据此QA
  2. Determining if HKEY_LOCAL_MACHINE\\SOFTWARE\\PostgreSQL\\ registry exists, and if it does, seeing the versions and service names under the \\Installations\\ and \\Services\\ subs 确定是否存在HKEY_LOCAL_MACHINE \\ SOFTWARE \\ PostgreSQL \\注册表,如果存在,请在\\ Installations \\和\\ Services \\子目录下查看版本和服务名称

However, I am more concerned about the connectivity part. 但是,我更关心连接性部分。 If my application (to be written in C#) is dependent on a database name "MyProgram," how would it be possible to create a database in PostgreSQL and with what credentials? 如果我的应用程序(用C#编写)依赖于数据库名称“ MyProgram”,那么如何在PostgreSQL中创建数据库并使用什么凭据?

From my understanding, the way to go would be to attempt to log in as the default superuser, postgres, to the default database, postgres, and create a new user and database from that connection. 据我了解,方法是尝试以默认超级用户postgres登录到默认数据库postgres,并从该连接创建新的用户和数据库。 But, the password for postgres user is set during by the user/program that is initially is installing PostgreSQL. 但是,postgres用户的密码是由最初安装PostgreSQL的用户/程序设置的。

How to go about this? 怎么办呢? Any help is much appreciated! 任何帮助深表感谢!

IMPORTANT NOTE: This may not be the most ethical/proper way of doing this, but it appears to get the job done. 重要说明:这可能不是最合乎道德/最正确的方法,但是看来可以完成工作。

After numerous hours of digging, using the registry key on Windows is the best methodology, it seems, to determine if the version of PostgreSQL you intend to install, if it is already installed. 经过数小时的研究,似乎最好的方法是使用Windows上的注册表项来确定您打算安装的PostgreSQL版本(如果已安装)。

Assuming that I am running on the Windows system with Administrative rights, in theory, I should be able to change the login credential requirements of the pg_hba.conf file in the data directory (ie cluster) that already exists to allow myself to create the database(s) and user(s) I need to, before reverting the credential requirement settings to what they originally were. 假设我在具有管理权限的Windows系统上运行,从理论上讲,我应该能够更改数据目录(即群集)中pg_hba.conf文件的登录凭据要求,以允许自己创建数据库将凭据要求设置恢复为最初的设置之前,我需要这些用户。

So, the answer I have come to the conclusion with is: 因此,我得出的结论是:

  1. Determine if PostgreSQL is already installed or not. 确定是否已经安装了PostgreSQL。 Look at the HKEY_LOCAL_MACHINE\\SOFTWARE\\PostgreSQL\\Installation\\[version] registry, where [version] is formatted as postgresql-[32 or 64 bit]-[PostgreSQL version] , eg postgresql-x64-12 ). 查看HKEY_LOCAL_MACHINE\\SOFTWARE\\PostgreSQL\\Installation\\[version]注册表,其中[version]的格式设置为postgresql-[32 or 64 bit]-[PostgreSQL version] ,例如postgresql-x64-12 )。 If the registry exists, then data should exist for the Data Directory value... obtain that data, and that's where the cluster is located. 如果注册表存在,则数据Data Directory值的数据应存在...获取该数据,这就是群集所在的位置。

Step [2] and on are obviously for when PostgreSQL is already installed. 步骤[2]及之后的步骤显然适用于已经安装PostgreSQL的情况。

  1. Make a copy of the pg_hba.conf file in the cluster directory provided by the Data Directory value from step [1]. 在步骤[1]的“ Data Directory值提供的群集目录中复制pg_hba.conf文件。

This will be the file we restore to after we are done. 这将是我们完成后还原到的文件。 Save file to a temporary directory, such as Desktop. 将文件保存到一个临时目录,例如Desktop。

  1. In the pg_hba.conf file in the cluster directory, change all connection types' methods to trust 在集群目录的pg_hba.conf文件中,将所有连接类型的方法更改为trust

Example: 例:

# TYPE  DATABASE        USER            ADDRESS                 METHOD
host    all             all             127.0.0.1/32            trust
  1. Restart Postgres' Windows service with the following command: pg_ctl.exe restart -D <cluster directory> 使用以下命令重新启动Postgres的Windows服务: pg_ctl.exe restart -D <cluster directory>

NOTE: pg_ctl.exe is located under the \\bin\\ folder of Postgres' installation directory. 注意: pg_ctl.exe位于Postgres安装目录的\\ bin \\文件夹下。

Example: C:\\Program Files\\PostgreSQL\\12\\bin\\pg_ctl.exe restart -D "C:\\Program Files\\PostgreSQL\\12\\data\\" 示例: C:\\Program Files\\PostgreSQL\\12\\bin\\pg_ctl.exe restart -D "C:\\Program Files\\PostgreSQL\\12\\data\\"

  1. Connect to the cluster and issue the command to create a superuser role for your needs with the following command: psql -h 127.0.0.1 -p 5432 -d postgres -c "CREATE ROLE <role name> LOGIN SUPERUSER PASSWORD '<password>'; 连接到集群,并使用以下命令发出命令来创建您需要的超级用户角色psql -h 127.0.0.1 -p 5432 -d postgres -c "CREATE ROLE <role name> LOGIN SUPERUSER PASSWORD '<password>';

In the above command, I have the cluster running on the local computer (ie localhost, IP address 127.0.0.1) on port # 5432 (default), connecting to the default database postgres and issuing the command to create a role with whatever role name provided in place of <role name> , with SUPERUSER rights and the password provided in place of <password> . 在上面的命令中,我让群集在本地计算机(即localhost,IP地址127.0.0.1)上的端口号5432(默认)上运行,连接到默认的数据库postgres并发出命令以使用任何角色名称创建角色在<role name> ,并具有SUPERUSER权限和在<password>处提供的<password>

Since one HAS to connect to a database, I am connecting to the default one postgres , otherwise template0 and template1 are default databases that could also be utilized. 由于有一个HAS连接到数据库,因此我要连接到默认的一个postgres ,否则template0template1是也可以使用的默认数据库。

  1. Connect to the cluster and issue the command to create the needed database for your needs with the following command: psql -h 127.0.0.1 -p 5432 -d postgres -c "CREATE DATABASE <database name>; 连接到集群并发出命令,使用以下命令创建满足您需要的数据库psql -h 127.0.0.1 -p 5432 -d postgres -c "CREATE DATABASE <database name>;
  2. Replace the pg_hba.conf file with the original 用原始文件替换pg_hba.conf文件
  3. Restart Postgres' Windows service with the following command: pg_ctl.exe restart -D <cluster directory> 使用以下命令重新启动Postgres的Windows服务: pg_ctl.exe restart -D <cluster directory>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM