Yao Lirong's Blog

Windows下配置PostgreSQL

2020/09/05

如果你在纠结要不要装,别装了吧,对自己好一点

  1. 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.

  2. Run the pg_env.bat under installation folder or the following env.vbs file if that bat doesn’t work

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    on 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"
  3. Initialize database by running initdb -D C:\Hacking\PostgreSQL\data

  4. Start Server by running pg_ctl -D C:\Hacking\PostgreSQL\data start

  5. 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)

  6. 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.

  7. Allow operations on database without password by changing authentication method in PostgreSQL\data\pg_hba.conf all from md5 to trust: (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 trust
  8. Now when we run command createdb db, we may get a message Fatel Error: User "harmo" does not exist. This is a similar problem as in step 6. We want to create a user called “harmo” by running createuser -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.

  9. Now PostgreSQL is ready to use.

过了一天服务启动不起来了,查了一堆没有结果,我还用你妈了个嗨,小爷一个周末就整你这傻逼玩意,草了真的是

好像是启动 pgAdmin 之后,又可以用了。刚刚启动的服务本身就是为了让 local server 运作的,而 pgAdmin 可以帮我们完成这个任务。于是我们登录进 pgAdmin 就相当于让那个服务运作起来了,也就可以正常在终端中使用命令了

Reference

  1. 主要:Windows下在命令行安装postgresql,并注册成window服务
  2. Windows上PostgreSQL安装配置教程
  3. Windows下Postgresql下载与配置方法
  4. postgresql 口令: psql: 致命错误: 用户 认证失败
  5. 本机psql设置需要/不需要密码
  6. Postgres psql: 致命错误: 角色 “postgres” 不存在
CATALOG
  1. 1. Reference