如果你在纠结要不要装,别装了吧,对自己好一点
Download and Install PostgreSQL. During the process, PostgreSQL will ask you to create a username and password, the default username is “postgres” and password is up to you.
Run the
pg_env.bat
under installation folder or the followingenv.vbs
file if thatbat
doesn’t work1
2
3
4
5
6
7
8
9
10
11on error resume next
set sysenv=CreateObject("WScript.Shell").Environment("system") '系统环境变量的数组对象
Path = CreateObject("Scripting.FileSystemObject").GetFolder(".").Path
'添加变量
sysenv("PGHOME")="C:\Hacking\PostgreSQL" '!!!change to your own directory!!!'
sysenv("PGHOST")="localhost"
sysenv("Path")=sysenv("PGHOME")+"\bin;"+sysenv("Path")
sysenv("PGLIB")=sysenv("PGHOME")+"\lib"
sysenv("PGDATA")=sysenv("PGHOME")+"\data"
wscript.echo "PostgreSQL Environment Variable Successfully set"Initialize database by running
initdb -D C:\Hacking\PostgreSQL\data
Start Server by running
pg_ctl -D C:\Hacking\PostgreSQL\data start
Register a server service by running
pg_ctl register -N "PostgreSQL" -D C:\Hacking\PostgreSQL\data
(If not working, run terminal as administrator) You should then be able to see a service called “PostgreSQL” in Windows Services (Win+R services.msc)Create Database by running
createdb -U postgres <DatabaseName>
and entering the password you entered in step 1.Note it will give a “Password Authentication Failure” if you try
createdb <DatabaseName>
and enter the password that way. That is because PostgreSQL uses your computer username as default and no such use is registered in PostgreSQL in the first place. You have to use-U
option to specify the user you want to log in as.Allow operations on database without password by changing authentication method in
PostgreSQL\data\pg_hba.conf
all frommd5
totrust
: (might take effect after a reboot)1
2
3
4
5
6
7
8
9
10# TYPE DATABASE USER ADDRESS METHOD
# IPv4 local connections:
host all all 127.0.0.1/32 trust (was md5)
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trustNow when we run command
createdb db
, we may get a messageFatel Error: User "harmo" does not exist
. This is a similar problem as in step 6. We want to create a user called “harmo” by runningcreateuser -s -r -U postgres harmo
.Note simply
creatuser -s -r harmo
will not work because the default user to use to create such a user harmo is also harmo, which doesn’t exist in the first place. What a stupid logic PostgreSQL applies.Now PostgreSQL is ready to use.
过了一天服务启动不起来了,查了一堆没有结果,我还用你妈了个嗨,小爷一个周末就整你这傻逼玩意,草了真的是
好像是启动 pgAdmin
之后,又可以用了。刚刚启动的服务本身就是为了让 local server 运作的,而 pgAdmin
可以帮我们完成这个任务。于是我们登录进 pgAdmin
就相当于让那个服务运作起来了,也就可以正常在终端中使用命令了