2021年9月

Windows 10 安装 PostgreSQL 13 过程中报错 PostgreSQL: The database cluster initialization failed。
安装完成后尝试使用以下命令启动 PostgreSQL:

pg_ctl.exe -D "D:\Program Files\PostgreSQL\13\data" start

出现以下的错误提示:

pg_ctl: 目录 "D:/Program Files/PostgreSQL/13/data"不是一个数据库集群目录

同时在 Windows 的系统服务中也无法找到 PostgreSQL 的服务 postgresql-x64-13

一番搜索后发现是安装过程中的用户权限不足导致,可以尝试使用以下步骤解决(需以管理员的权限运行命令行):

  1. 运行 PostgreSQL 安装目录下的 uninstall-postgresql.exe 卸载 PostgreSQL:
  2. 如果 postgres 用户已存在,先删除该用户:

    net user postgres /delete
  3. 创建 postgres 用户并记住设置的用户密码:

    net user /add postgres <password>
  4. postgres 用户加入到 Administrators 用户组:

    net localgroup administrators postgres /add
  5. postgres 用户加入到 Power Users 用户组:

    net localgroup "power users" postgres /add
  6. 使用 postgres 用户启用一个新的命令行窗口:

    runas /user:postgres cmd.exe
  7. 在新的命令行窗口中运行安装程序(先使用cd命令进入到安装程序postgresql-13.4-1-windows-x64.exe所在的目录再运行以下命令):

    .\postgresql-13.4-1-windows-x64.exe
  8. 安装完成后,从Administrators用户组中删除postgres用户:

    net localgroup administrators postgres /delete
  9. 禁用postgres用户:
    打开计算机管理,选择本地用户和组中的用户,找到用户postgres,右键该用户选择属性打开属性面板,勾选面板中的账户已禁用(B)然后点击确定

重新安装成功后,可以通过安装目录下 \PostgreSQL\13\pgAdmin 4\bin\pgAdmin4.exe的pgAdmin客户端和安装时设置的密码来连接postgresql数据库。

PostgreSQL 13 安装文件 postgresql-13.4-1-windows-x64.exe 下载链接: https://get.enterprisedb.com/postgresql/postgresql-13.4-1-windows-x64.exe

参考内容来源:https://dba.stackexchange.com/questions/10241/postgresql-the-database-cluster-initialization-failed

Python 2 与 Python 3 的主要区别

对比选项Python 3Python 2
printprint("Hello")print "Hello"
整数除法除法得到的值是float类型除法得到的值是int类型
Unicode字符串默认是 Unicode 保存,需要通过b'string'指定为 bytes默认为 bytes 需通过u'string'指定为Unicode
变量泄露变量值不会改变全局变量的值会在 for-loop 的内部发生改变
兼容从 Python 2 迁移到 Python 3 并不困难Python 3 与 Python 2 不向后兼容

Python 2 中 x 的值在列表推导式中发生改变

>>> x = 'hello'
>>> print(x)
hello
>>> vowels = [x for x in 'aeiou']
>>> print(vowels)
['a', 'e', 'i', 'o', 'u']
>>> print(x)
u

Python 3 中 x 的值未发生改变

>>> x = 'hello'
>>> print(x)
hello
>>> vowels = [x for x in 'aeiou']
>>> print(vowels)
['a', 'e', 'i', 'o', 'u']
>>> print(x)
hello

Python 2 与 Python 3 均会泄露:

>>> i = 0
>>> print(i)
0
>>> for i in [1, 2, 3]:
...     pass
...
>>> print(i)
3

参考内容:
Python 2 vs Python 3: What’s the Difference Between Python 2.x and Python 3.x?
Leaked variables in list comprehension

Strings, Bytes, and Unicode in Python 2 and 3

Python 2 vs Python 3 String Handling

"Hello World"
Python 2a "str" object stored as bytes. Prefix it with "u" to get a "unicode" object which is stored as Unicode
Python 3a "str" object that stores Unicode. Prefix it with "b" to get a bytes object or use .encode.

Using the Unicode Sandwich model to handle Unicode

Unicode Sandwich model.png
当你的程序接收到 byte strings 后尽快将其解码为 Unicode,在你的程序中所有的对这些 strings 的操作都将会在 unicode 的形式下进行,最后将其编码回 byte strings 并输出。

Pragmatic Unicode - Unipain

Str vs Unicode
stra sequence of bytes
unicodea sequence of code points (unicode)

unicode .encode() -> bytes
bytes .decode() -> unicode

转换时的错误处理策略

strict(default)/replace/xmlcharrefreplace/ignore

my_unicode = u'Hi \u2119\u01b4\u2602\u210c\xf8\u1f24'
my_unicode.encode('ascii', 'replace')
>>> 'Hi ??????'

my_unicode.encode('ascii', 'xmlcharrefreplace')
>>> 'Hi &#8473;&#436;&#9730;&#8460;&#248;&#7972;'

my_unicode.encode('ascii', 'ignore')
>>> 'Hi '

快速剔除字符串中的非ascii字符串: string.encode('utf-8').decode('ascii', 'ignore')

UTF-8编码字节含义

  • 对于UTF-8编码中的任意字节B,如果B的第一位为0,则B独立的表示一个字符(ASCII码);
  • 如果B的第一位为1,第二位为0,则B为一个多字节字符中的一个字节(非ASCII字符);
  • 如果B的前两位为1,第三位为0,则B为两个字节表示的字符中的第一个字节;
  • 如果B的前三位为1,第四位为0,则B为三个字节表示的字符中的第一个字节;
  • 如果B的前四位为1,第五位为0,则B为四个字节表示的字符中的第一个字节;

因此,对UTF-8编码中的任意字节,根据第一位,可判断是否为ASCII字符;根据前二位,可判断该字节是否为一个字符编码的第一个字节;根据前四位(如果前两位均为1),可确定该字节为字符编码的第一个字节,并且可判断对应的字符由几个字节表示;根据前五位(如果前四位为1),可判断编码是否有错误或数据传输过程中是否有错误。

GBK编码的中文可以依据读音进行正则检索,因为GBK编码是按读音排序的。(部分多音字)
UTF-8编码的中文可以依据部首进行正则检索,UTF-8不是按读音排序的,是按照部首排序的。

参考内容:
Strings, Bytes, and Unicode in Python 2 and 3
Pragmatic Unicode - Unipain
The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
Python 3 Unicode and Byte Strings
UTF-8 - Wikipedia