【Linux】【PostgreSQL】PostgreSQL安装

前言

  PostgreSQL是一种特性很是齐全的自由软件的对象-关系型数据库管理系统(ORDBMS),是以加州大学计算机系开发的POSTGRES,4.2版本为基础的对象关系型数据库管理系统。POSTGRES的许多领先概念只是在比较迟的时候才出如今商业网站数据库中。PostgreSQL支持大部分的SQL标准而且提供了不少其余现代特性,如复杂查询、外键、触发器、视图、事务完整性、多版本并发控制等。一样,PostgreSQL也能够用许多方法扩展,例如经过增长新的数据类型、函数、操做符、汇集函数、索引方法、过程语言等。另外,由于许可证的灵活,任何人均可以以任何目的无偿使用、修改和分发PostgreSQL。linux

安装

1.下载PostgreSQL   https://www.enterprisedb.com/download-postgresql-binariessql

 下载相应版本经过ftp上传到服务器的/home路径下,而后解压,获得pgsql文件夹数据库

2.建立pgsql用户并建立密码:bootstrap

1 [root@localhost home]# useradd postgres 2 [root@localhost home]# passwd postgres 3 更改用户 postgres 的密码 。 4 新的 密码: 5 无效的密码: 密码包含用户名在某些地方 6 从新输入新的 密码: 7 passwd:全部的身份验证令牌已经成功更新。

注意:这里设置密码是linux用户postgres的登陆密码,不是pgsql服务器的密码bash

3.建立pgsql的数据目录并赋权限服务器

1 # 当前目录在/home下 2 [root@localhost home]# pwd 3 /home 4 
5 # 建立/home/pgsql/pgsql_data 6 [root@localhost home]# mkdir pgsql/pgsql_data 7 
8 # 赋予pgsql_data文件夹 postgres用户权限 9 [root@localhost home]# chown postgres:postgres pgsql/pgsql_data

4.初始化数据库并发

切换到postgres用户来操做数据库,pgsql数据库就以postgres为默认用户,执行: su - postgres 切换tcp

 1 # 切换到postgres用户  2 [root@localhost home]# su postgres  3 
 4 -bash-4.2$ pwd  5 /home  6 
 7 # 初始化数据库  8 -bash-4.2$ /home/pgsql/bin/initdb -D /home/pgsql/pgsql_data  9 The files belonging to this database system will be owned by user "postgres". 10 This user must also own the server process. 11 
12 The database cluster will be initialized with locale "zh_CN.UTF-8". 13 The default database encoding has accordingly been set to "UTF8". 14 initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8" 15 The default text search configuration will be set to "simple". 16 
17 Data page checksums are disabled. 18 
19 fixing permissions on existing directory /home/pgsql/pgsql_data ... ok 20 creating subdirectories ... ok 21 selecting default max_connections ... 100 22 selecting default shared_buffers ... 128MB 23 selecting default timezone ... Asia/Shanghai 24 selecting dynamic shared memory implementation ... posix 25 creating configuration files ... ok 26 running bootstrap script ... ok 27 performing post-bootstrap initialization ... ok 28 syncing data to disk ... ok 29 
30 WARNING: enabling "trust" authentication for local connections 31 You can change this by editing pg_hba.conf or using the option -A, or 32 --auth-local and --auth-host, the next time you run initdb. 33 
34 Success. You can now start the database server using: 35 
36     /home/pgsql/bin/pg_ctl -D /home/pgsql/pgsql_data -l logfile start

5.启动数据库ide

初始化数据库时,最后打印出启动数据库命令:   /home/pgsql/bin/pg_ctl -D /home/pgsql/pgsql_data -l logfile start函数

这里-l指定日志文件位置,这里直接输出在家目录下的logfile中,这个能够本身指定,这里-D指定数据目录,默认若是不加数据目录直接报错找不到,能够刚才说的环境变量配置文件中~/.bash_profile加入一行: export PGDATA=/home/pgsql/pgsql_data 而后source进去便可,这样pgsql会自动去找PGDATA环境变量值,找不到才会报错
启动以后此时执行: ps -ef | grep postgres 就能够看到相关进程以下:

 1 -bash-4.2$ /home/pgsql/bin/pg_ctl -D /home/pgsql/pgsql_data -l /home/pgsql/logfile start  2 waiting for server to start.... done  3 server started

# 查看postgres进程
4 -bash-4.2$ ps -ef | grep postgres 5 root 20480 4206 0 16:10 pts/0 00:00:00 su - postgres 6 postgres 20481 20480 0 16:10 pts/0 00:00:00 -bash 7 postgres 20544 1 0 16:16 pts/0 00:00:00 /home/pgsql/bin/postgres -D /home/pgsql/pgsql_data 8 postgres 20546 20544 0 16:16 ? 00:00:00 postgres: checkpointer process 9 postgres 20547 20544 0 16:16 ? 00:00:00 postgres: writer process 10 postgres 20548 20544 0 16:16 ? 00:00:00 postgres: wal writer process 11 postgres 20549 20544 0 16:16 ? 00:00:00 postgres: autovacuum launcher process 12 postgres 20550 20544 0 16:16 ? 00:00:00 postgres: stats collector process 13 postgres 20551 20544 0 16:16 ? 00:00:00 postgres: bgworker: logical replication launcher 14 postgres 20552 20481 0 16:16 pts/0 00:00:00 ps -ef 15 postgres 20553 20481 0 16:16 pts/0 00:00:00 grep --color=auto postgres

pgsql默认的端口号为5432

-bash-4.2$ netstat -tunlp | grep 5432 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 20544/postgres tcp6 0 0 ::1:5432 :::* LISTEN 20544/postgres 

6.开启远程访问

# 找到pgsql_data文件夹 [root@localhost pgsql_data]# pwd /home/pgsql/pgsql_data [root@localhost pgsql_data]# ls base pg_commit_ts pg_hba.conf pg_logical pg_notify pg_serial pg_stat pg_subtrans pg_twophase pg_wal postgresql.auto.conf postmaster.opts global pg_dynshmem pg_ident.conf pg_multixact pg_replslot pg_snapshots pg_stat_tmp pg_tblspc PG_VERSION pg_xact postgresql.conf postmaster.pid # vi修改postgresql.conf [root@localhost pgsql_data]# vi postgresql.conf 

将 listen_addresses 前的#删掉,而后把localhost改为 *

重启便可。

/home/pgsql/bin/pg_ctl -D /home/pgsql/pgsql_data -l /home/pgsql/logfile restart
1 # 修改重启前 127.0.0.1:5432 2 [root@localhost pgsql_data]# netstat -tunlp | grep 5432 3 tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 20544/postgres 4 tcp6 0 0 ::1:5432 :::* LISTEN 20544/postgres 5 
6 # 修改重启后 0.0.0.0:5432 7 [root@localhost pgsql_data]# netstat -tunlp | grep 5432 8 tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 20595/postgres 9 tcp6       0      0 :::5432                 :::*                    LISTEN      20595/postgres

7.链接数据库

1 [root@localhost bin]# /home/pgsql/bin/psql -h 127.0.0.1 -d postgres -U postgres -p 5432 2 psql.bin (10.10) 3 Type "help" for help. 4 
5 postgres=#

其中-h参数指定服务器地址,默认为127.0.0.1,默认不指定便可,-d指定链接以后选中的数据库,默认也是postgres,-U指定用户,默认是当前用户,-p 指定端口号,默认是"5432"

查看当前数据库列表

postgres=#  \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres (3 rows)

默认postgres,template0和1这3个库是不容许操做的,建立新的数据库执行: CREATE DATABASE test WITH OWNER=postgres ENCODING='UTF-8'; 这样就建立好了数据库test,而后能够执行命令 \c test 切换当前数据库为test,而后执行 \d 能够查看当前数据库下的全部表:

# 建立test数据库
postgres=# CREATE DATABASE test WITH OWNER=postgres ENCODING='UTF-8'; CREATE DATABASE
# 查看全部数据库列表 postgres=# \l List of databases Name | Owner | Encoding | Collate | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | template0 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres + | | | | | postgres=CTc/postgres test | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | (4 rows)
# 切换到test数据库 postgres=# \c test You are now connected to database "test" as user "postgres".

# 查看test数据库的全部表 test=# \d Did not find any relations.

建立一个表

# 建立一个表student test=# CREATE TABLE student ( test(# id integer NOT NULL, test(# name character(32), test(# number char(5), test(# CONSTRAINT student_pkey PRIMARY KEY (id) test(# ); CREATE TABLE # 查看student表结构 test=#  \d student Table "public.student" Column | Type | Collation | Nullable | Default --------+---------------+-----------+----------+--------- id | integer | | not null | name | character(32) | | | number | character(5) | | | Indexes: "student_pkey" PRIMARY KEY, btree (id) # 插入student表一条数据 test=# INSERT INTO student (id,name,number) VALUES (1,'张三','1023'); INSERT 0 1 # 查看插入的数据 test=# SELECT * FROM student WHERE id=1; id | name | number ----+------------------------------------+-------- 1 | 张三 | 1023 (1 row) # \q退出pgsql test=# \q [root@localhost bin]#