前言
今年个人搭建项目需要使用PostgreSQL12.2数据库,在这里分享下安装过程,有需要的童鞋可以参考下。
PostgreSQL是啥
PostgreSQL是自由的对象-关系型数据库服务器,在灵活的BSD风格许可证下发行。
更多知识,可以搜索:
下面将开始介绍从下载、安装到使用的说明。
1.下载
下载页面:https://www.postgresql.org/ftp/source/
$ wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.gz
2.操作系统Root用户安装依赖库
# yum install readline-devel# yum install zlib-devel
3.普通用户编译
$ tar zxf postgresql-12.2.tar.gz $ cd postgresql-12.2$ mkdir -p ~/3rd/postgresql-12.2$ ./configure --prefix=/home/testerzhang/3rd/postgresql-12.2$ make$ make install
4.环境变量
- 编辑环境变量
$ vim ~/.bash_profileexport PGHOME=$HOME/3rd/postgresql-12.2export PGDATA=$PGHOME/dataexport PATH=$PGHOME/bin:$PATH
- 生效环境变量
$ source ~/.bash_profile
- 验证
$ which psql~/3rd/postgresql-12.2/bin/psql$ which initdb~/3rd/postgresql-12.2/bin/initdb
5.初始化
$ initdbinitdb: error: no data directory specifiedYou must identify the directory where the data for this database systemwill reside. Do this with either the invocation option -D or theenvironment variable PGDATA.
如果没有配置环境变量PGDATA,就会报上面这个错,也可以指定[-D, --pgdata=]DATADIR location for this database cluster解决问题。
我们上面已经配置,直接执行就可以了。
$ initdb The files belonging to this database system will be owned by user "testerzhang".This user must also own the server process.The database cluster will be initialized with locale "zh_CN.UTF-8".The default database encoding has accordingly been set to "UTF8".initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"The default text search configuration will be set to "simple".Data page checksums are disabled.creating directory /home/testerzhang/3rd/postgresql-12.2/data ... okcreating subdirectories ... okselecting dynamic shared memory implementation ... posixselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting default time zone ... Asia/Shanghaicreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... oksyncing data to disk ... okinitdb: warning: enabling "trust" authentication for local connectionsYou can change this by editing pg_hba.conf or using the option -A, or--auth-local and --auth-host, the next time you run initdb.Success. You can now start the database server using: pg_ctl -D /home/testerzhang/3rd/postgresql-12.2/data -l logfile start
看到上面信息代表成功了。
6.访问控制配置文件pg_hba.conf
一般配置文件目录在data目录,具体看下面例子:
$ pwd/home/testerzhang/3rd/postgresql-12.2$ find . -name pg_hba.conf./data/pg_hba.conf
下面开始编辑配置文件
$ vim ./data/pg_hba.conf# "local" is for Unix domain socket connections onlylocal all all trust# IPv4 local connections:#host all all 127.0.0.1/32 trusthost all all 0.0.0.0/0 md5# IPv6 local connections:host all all ::1/128 trust# Allow replication connections from localhost, by a user with the# replication privilege.local replication all trusthost replication all 127.0.0.1/32 trust
这里注释原来的记录加入了:修改为0.0.0.0/0,加密方式改为md5,就表示需要密码访问
host all all 0.0.0.0/0 md5
7.主配置文件postgresql.conf
listen_addresses修改为监听整个网络,请根据实际修改。
$ vim ./data/postgresql.conf#listen_addresses = 'localhost' # what IP address(es) to listen on; # comma-separated list of addresses; # defaults to 'localhost'; use '*' for all # (change requires restart)listen_addresses = '*'
8.启动
-l表示日志文件目录,通常需要指定,所以我们在/usr/local/postgresql根目录下再创建一个log目录用来存放日志文件(注意别忘记赋予可写的权限)。
$ pg_ctl -D /home/testerzhang/3rd/postgresql-12.2/data -l logfile start
9.连接PostgreSQL
创建当前系统普通用户的数据库:
$ createdb testerzhang
连接数据库,默认连接跟系统用户名一样的数据库。
$ psql psql (12.2)Type "help" for help.testerzhang=# testerzhang-# q
如果需要指定连接到某个数据库(test_db),可以用psql -d test_db 。
10.修改用户名的密码
testerzhang=# select * from pg_user; usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls | passwd | valuntil | useconfig ---------+----------+-------------+----------+---------+--------------+----------+----------+----------- testerzhang | 10 | t | t | t | t | ******** | | (1 row)
修改密码
testerzhang=# alter user testerzhang password 'qwerty';ALTER ROLE
11.关闭服务
$ pg_ctl stop
12.常用命令
- 查看所有数据库
l
- 切换数据库: c 参数为数据库名
c database
- 切换用户: c 参数为用户名
c username
- 查看所有表
dt
- d 表名 —— 得到表结构
d table
好了,今天就分享到这里了,你Get到了吗?
我是testerzhang,喜欢本文的童鞋,可以关注我+收藏,不明白的地方也可以评论留言。