PostgreSQL 教程

查询

连接

高级部分

程序连接接口

PostgreSQL 配置

PostgreSQL 可视化工具

PostgreSQL 笔记

original icon
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.knowledgedict.com/tutorial/postgresql-install.html

PostgreSQL 在各个平台上的完整安装


本文主要介绍 PostgreSQL 在各大主流平台的安装步骤,主要是市面上的常用操作系统发行版上的安装说明。

Linux 上安装

centos 上安装

  1. 安装源库(若之前安装别的软件时,执行过该命令,跳过):

    sudo yum -y install epel-release
  2. 安装 postgresql 客户端和服务:

    yum install postgresql postgresql-server
  3. 初始化 postgresql:

    /usr/bin/postgresql-setup initdb
  4. 注册 postgresql 服务:

    systemctl enable postgresql
  5. 验证 postgresql 服务:

    systemctl status postgresql
  6. 开启 postgresql 服务:

    sudo systemctl start postgresql
  7. 查看 postgresql 服务状态:

    systemctl status postgresql.service
  8. 重启 postgresql 服务:

    systemctl restart postgresql.service

安装完 PostgreSQL 之后,PostgreSQL 会在操作系统中创建一个名为 “postgres” 的用户(注意,这是操作系统级别的用户);此外 PostgreSQL 的默认用户名和数据库也是 “postgres”(这是数据库层面的用户), 不过没有默认密码。PostgreSQL 可以以默认用户登录,也可以创建新用户名。

  1. 切换 postgres 账号:

    su postgres

    显示类似如下:

    bash-4.4$
  2. 登陆 PostgreSQL:

    psql

    具体操作类似如下:

    bash-4.4$ psql
    psql (10.17)
    Type "help" for help.
    
    postgres=# 
  3. 给默认用户 “postgres” 设置密码:

    # \password postgres

    具体操作类似如下:

    postgres=#
    postgres-# # \password postgres
    Enter new password: 
    Enter it again: 
    postgres-#

Mac OS 上安装

通过 homebrew 安装

  1. 首先通过 brew search 查看可安装的 postgresql:

    knowledgedict@MacOS ~ $ brew search postgresql
    ==> Formulae
    postgresql       postgresql@10    postgresql@11    postgresql@12    postgresql@9.4   postgresql@9.5   postgresql@9.6
    ==> Casks
    navicat-for-postgresql
  2. 选中安装的版本,brew install 笔者以版本 10 为例:

    knowledgedict@MacOS ~ $ brew install postgresql@10
    ==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/icu4c-70.1.big_sur.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/ca-certificates-2022-04-26.all.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/openssl%401.1-1.1.1p.big_sur.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/readline-8.1.2.big_sur.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Downloading https://mirrors.ustc.edu.cn/homebrew-bottles/postgresql%4010-10.21.big_sur.bottle.tar.gz
    ######################################################################## 100.0%
    ==> Installing dependencies for postgresql@10: icu4c, ca-certificates, openssl@1.1 and readline
    ==> Installing postgresql@10 dependency: icu4c
    ==> Pouring icu4c-70.1.big_sur.bottle.tar.gz
    🍺  /usr/local/Cellar/icu4c/70.1: 261 files, 74.4MB
    ==> Installing postgresql@10 dependency: ca-certificates
    ==> Pouring ca-certificates-2022-04-26.all.bottle.tar.gz
    ==> Regenerating CA certificate bundle from keychain, this may take a while...
    🍺  /usr/local/Cellar/ca-certificates/2022-04-26: 3 files, 215.6KB
    ==> Installing postgresql@10 dependency: openssl@1.1
    ==> Pouring openssl@1.1-1.1.1p.big_sur.bottle.tar.gz
    🍺  /usr/local/Cellar/openssl@1.1/1.1.1p: 8,097 files, 18.5MB
    ==> Installing postgresql@10 dependency: readline
    ==> Pouring readline-8.1.2.big_sur.bottle.tar.gz
    🍺  /usr/local/Cellar/readline/8.1.2: 48 files, 1.6MB
    ==> Installing postgresql@10
    ==> Pouring postgresql@10-10.21.big_sur.bottle.tar.gz
    ==> /usr/local/Cellar/postgresql@10/10.21/bin/initdb --locale=C -E UTF-8 /usr/local/var/postgresql@10
    ==> Caveats
    This formula has created a default database cluster with:
      initdb --locale=C -E UTF-8 /usr/local/var/postgresql@10
    For more details, read:
      https://www.postgresql.org/docs/10/app-initdb.html
    
    postgresql@10 is keg-only, which means it was not symlinked into /usr/local,
    because this is an alternate version of another formula.
    
    If you need to have postgresql@10 first in your PATH, run:
      echo 'export PATH="/usr/local/opt/postgresql@10/bin:$PATH"' >> /Users/honey/.bash_profile
    
    For compilers to find postgresql@10 you may need to set:
      export LDFLAGS="-L/usr/local/opt/postgresql@10/lib"
      export CPPFLAGS="-I/usr/local/opt/postgresql@10/include"
    
    
    To restart postgresql@10 after an upgrade:
      brew services restart postgresql@10
    Or, if you don't want/need a background service you can just run:
      /usr/local/opt/postgresql@10/bin/postgres -D /usr/local/var/postgresql@10
    ==> Summary
    🍺  /usr/local/Cellar/postgresql@10/10.21: 1,712 files, 25.5MB
    ....
    ....
    ....
  3. 根据提示将命令设置在 path 中:

    echo 'export PATH="/usr/local/opt/postgresql@10/bin:$PATH"' >> /Users/honey/.bash_profile

    并 source 激活配置:

    source ~/.bash_profile
  4. 设置开机自动启动:

    brew services restart postgresql@10
  5. 与客户端安装不同的是,homebrew 安装的是不会创建默认账号 postgres 的,自然也不会有权限,所以要在执行以下步骤创建并给用户赋权:

    createdb
  6. 连接 postgresql,不指定用户名和数据库默认是当前登陆系统账号同名的用户与数据库:

    psql
  7. 创建用户:

    CREATE USER postgres WITH PASSWORD '123456';
  8. 删除默认生成的 postgres 库:

    DROP DATABASE postgres;
  9. 创建属于 postgres 用户的数据库,注意:这里创建数据库时,后面还跟了 owner postgres,并在之后将数据库的权限都赋给该用户了,这样该用户才能自由的操作该数据库:

    CREATE DATABASE postgres OWNER postgres;
  10. 将所有权限赋给 postgres:

    GRANT ALL PRIVILEGES ON DATABASE postgres to postgres;
  11. 给 postgres 用户添加创建数据库的属性:

    ALTER ROLE postgres CREATEDB;

    之后就可以用 postgres 用户来创建并管理其他数据库了。

如果想指定 postgres 用户登录,可以 psql -u postgres

如果要彻底删除 Mac OS 上的 postgresql 请参考此文 - Mac OS 上 PostgreSQL 彻底卸载