【linux】【PostgreSQL】PostgreSQL安装

前言

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

安装

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

【linux】【PostgreSQL】PostgreSQL安装_第1张图片

 下载相应版本通过ftp上传到服务器的/home路径下,然后解压,得到pgsql文件夹

2.创建pgsql用户并创建密码:

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

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

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 切换

 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.启动数据库

初始化数据库时,最后打印出启动数据库命令:   /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]# 

转载于:https://www.cnblogs.com/jxd283465/p/11550745.html

你可能感兴趣的:(【linux】【PostgreSQL】PostgreSQL安装)