postgres 前言
PostgreSQL 是一个功能强大的开源对象关系数据库系统,拥有超过 35 年的积极开发经验 这为其赢得了可靠性、功能稳健性和性能的良好声誉。
通过官方文档可以找到大量描述如何安装和使用 PostgreSQL 的信息。 开源社区提供了许多有用的地方来熟悉PostgreSQL, 了解其运作方式,并寻找职业机会。了解更多有关 如何与社区互动。
download postgres
postgres all | pgadmin all | 文档 |
download | download | 参考 |
前提条件
- 开启wmi,配置网卡,参考
postgres 一键自动化部署
- 最终实现在线下载postgres,安装postgres,环境变量,初始化postgres数据库,用户密码配置,用户权限设置,远程连接设置,数据库创建postdb,vc依赖库安装,防火墙配置,安装包删除。
-
.\postgresql-15.3-1-windows-x64.exe --help #自动化部署参数查看,不同版本部署参数不同。
- postgres/Report@123 #postgres 超级管理员用户名密码
- postgresql_test/Report@123(用户拥有的权限LOGIN SUPERUSER CREATEDB CREATEROLE REPLICATION BYPASSRLS)
- 创建一个库postdb,这个只是测试,数据实际生产环境执行去除或者更改
- c:\postgresql\data #数据目录
- c:\postgresql #安装目录
- postgresql 5432 #postgresql端口
- c:\postgresql\data\pg_hba.conf #远程连接授权
- c:\postgresql\data\postgresql.conf #远程连接授权
- if 判断是否存在psql 命令 if (-not (Get-Command psql -ErrorAction SilentlyContinue))
- 依赖VisualC++ ,所有包含历史版下载
- increase indent:Tab
- decrease indent:Shift+Tab
powershell-install-postgresql.ps1
<# Powershell Install postgresql
+++++++++++++++++++++++++++++++++++++++++++++++++++++
+ _____ _____ _ _ _ +
+ | __ \ / ____| | | | |+
+ | |__) |____ _____ _ _| (___ | |__ ___| | |+
+ | ___/ _ \ \ /\ / / _ \ '__\___ \| '_ \ / _ \ | |+
+ | | | (_) \ V V / __/ | ____) | | | | __/ | |+
+ |_| \___/ \_/\_/ \___|_| |_____/|_| |_|\___|_|_|+
+ +++++++++++++++++++++++++++++++++++++++++++++++++++# postgresql all download
https://www.enterprisedb.com/downloads/postgres-postgresql-downloads# pgadmin all download
https://www.pgadmin.org/download/#Obtain parameters for automatically installing postgresql
.\postgresql-15.3-1-windows-x64.exe --help# Powershell Install postgresql
# .\powershell-install-postgresql.ps1
#> function Install-postgresql {if (-not (Get-Command psql -ErrorAction SilentlyContinue)) {if ($?) {$drive = "c:"$postgresql_temporary = "postgresql_temporary"$postgresql = "postgresql"$postgresql_data = "data"#postgresql configuration files$postgres_user = "postgres"$postgres_password = "Repot@123"$postgres_port = "5432"$postgres_install_language = "en"$pg_hba_conf = "pg_hba.conf"$postgresql_conf = "postgresql.conf"#postgresql packag$postgresql_exe = "postgresql-15.3-1-windows-x64.exe"$vc_redist_64 = "vc_redist.x64.exe"$vc_redist_86 = "vc_redist.x86.exe"Write-Host "Create a directory for storing postgresql temporary" -ForegroundColor GreenNew-Item -ItemType Directory $drive\$postgresql_temporary$postgresql_related_download = @("https://get.enterprisedb.com/postgresql/$postgresql_exe","https://aka.ms/vs/17/release/$vc_redist_64","https://aka.ms/vs/17/release/$vc_redist_86")foreach ($url in $postgresql_related_download) {$fileName = Split-Path $url -Leaf$filePath = Join-Path $drive\$postgresql_temporary $fileNameInvoke-WebRequest -Uri $url -OutFile $filePath
}Write-Host "install vc_redist x86 x64" -ForegroundColor GreenStart-Process -FilePath "$drive\$postgresql_temporary\$vc_redist_64" -ArgumentList {/q /install} -WaitStart-Process -FilePath "$drive\$postgresql_temporary\$vc_redist_86" -ArgumentList {/q /install} -WaitWrite-Host "install postgresql" -ForegroundColor GreenStart-Process -FilePath $drive\$postgresql_temporary\$postgresql_exe -ArgumentList "--mode unattended --installer-language $postgres_install_language --prefix $drive\$postgresql --datadir $drive\$postgresql\$postgresql_data --serverport $postgres_port --serviceaccount $postgres_user --servicepassword $postgres_password --superaccount $postgres_user --superpassword $postgres_password --create_shortcuts 1" -WaitWrite-Host "Create postgresql environment variables" -ForegroundColor Green$env:path += ";$drive\$postgresql\bin"setx PATH $env:path /MWrite-Host "pg_hba.conf changes the default ram-sha-256 authentication mode to trust authentication" -ForegroundColor Green(Get-Content -Path "$drive\$postgresql\$postgresql_data\$pg_hba_conf") -replace "scram-sha-256", "trust" | Set-Content -Path "$drive\$postgresql\$postgresql_data\$pg_hba_conf"Write-Host "postgresql Remote connection pg_hba.conf" -ForegroundColor Green(Get-Content "$drive\$postgresql\$postgresql_data\$pg_hba_conf") + "`host all all 0.0.0.0/0 md5" | Set-Content "$drive\$postgresql\$postgresql_data\$pg_hba_conf"Write-Host "postgresql what IP address(es) to listen on postgresql.conf" -ForegroundColor Green(Get-Content -Path "$drive\$postgresql\$postgresql_data\$postgresql_conf") | Foreach-Object {if ($_.ReadCount -eq 59) {"listen_addresses = '*'`n$_"} else {$_}} | Set-Content -Path "$drive\$postgresql\$postgresql_data\$postgresql_conf"Write-Host "restart postgresql service" -ForegroundColor GreenRestart-Service postgresql-x64-15Write-Host "check postgresql version" -ForegroundColor Greenpsql -VWrite-Host "Create a user and assign the user login permission, super user permission, super role permission, database creation permission, replication permission, and backup permission" -ForegroundColor Green$postgres_password;psql -U postgres -c "create USER postgresql_test WITH LOGIN SUPERUSER CREATEDB CREATEROLE REPLICATION BYPASSRLS PASSWORD 'Repot@123';"#Write-Host "Create a user and assign the user login permission, super user permission, super role permission, database creation permission, replication permission, and backup permission" -ForegroundColor Green#$postgres_password;psql -U postgres -c "create user postgresql_test; alter user postgresql_test WITH PASSWORD 'Report@123';"#$postgres_password;psql -U postgres -c "ALTER USER postgresql_test WITH LOGIN;"#$postgres_password;psql -U postgres -c "alter user postgresql_test WITH SUPERUSER;"#$postgres_password;psql -U postgres -c "alter user postgresql_test WITH CREATEROLE;"#$postgres_password;psql -U postgres -c "alter user postgresql_test WITH CREATEDB;"#$postgres_password;psql -U postgres -c "alter user postgresql_test WITH REPLICATION;"#$postgres_password;psql -U postgres -c "alter user postgresql_test WITH BYPASSRLS;"Write-Host "Create a database and add users to the database" -ForegroundColor Green$postgres_password;psql -U postgres -c "create database postdb owner postgresql_test;"$postgres_password;psql -U postgres -c "grant all privileges on database postdb TO postgresql_test;"Write-Host "firewall postgresql port" -ForegroundColor GreenNew-NetFirewallRule -DisplayName $postgresql -Direction Outbound -profile any -LocalPort $postgres_port -Protocol TCP -Action AllowNew-NetFirewallRule -DisplayName $postgresql -Direction Inbound -profile any -LocalPort $postgres_port -Protocol TCP -Action AllowWrite-Host "Delete related postgresql_temporary installation packages and temporary directories" -ForegroundColor GreenRemove-Item -Path "$drive\$postgresql_temporary\*.exe", "$drive\$postgresql_temporary" -Recurse -ForceWrite-Host "The postgresql Install Success..." -ForegroundColor Green} else {Write-Host "The postgresql Install Failed..." -ForegroundColor Redexit 1}} else {Write-Host "The postgresql Install already..." -ForegroundColor Yellow}
}function Main {Install-postgresql
}Main
执行安装
.\powershell-install-postgresql.ps1
输出结果展示
pgadmin 连接数据库
- postgres/Report@123 #postgres 超级管理员用户名密码
- postgresql_test/Report@123(用户拥有的权限LOGIN SUPERUSER CREATEDB CREATEROLE REPLICATION BYPASSRLS)