前言

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

安装

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

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

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

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

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

3.创建pgsql的数据目录并赋权限

 # 当前目录在/home下
[root@localhost home]# pwd
/home # 创建/home/pgsql/pgsql_data
[root@localhost home]# mkdir pgsql/pgsql_data # 赋予pgsql_data文件夹 postgres用户权限
[root@localhost home]# chown postgres:postgres pgsql/pgsql_data

4.初始化数据库

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

 # 切换到postgres用户
[root@localhost home]# su postgres -bash-4.2$ pwd
/home # 初始化数据库
-bash-4.2$ /home/pgsql/bin/initdb -D /home/pgsql/pgsql_data
The files belonging to this database system will be owned by user "postgres".
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. fixing permissions on existing directory /home/pgsql/pgsql_data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default timezone ... Asia/Shanghai
selecting dynamic shared memory implementation ... posix
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok WARNING: enabling "trust" authentication for local connections
You 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: /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 就可以看到相关进程如下:

 -bash-4.2$  /home/pgsql/bin/pg_ctl -D /home/pgsql/pgsql_data -l /home/pgsql/logfile start
waiting for server to start.... done
server started # 查看postgres进程
-bash-4.2$ ps -ef | grep postgres
root 20480 4206 0 16:10 pts/0 00:00:00 su - postgres
postgres 20481 20480 0 16:10 pts/0 00:00:00 -bash
postgres 20544 1 0 16:16 pts/0 00:00:00 /home/pgsql/bin/postgres -D /home/pgsql/pgsql_data
postgres 20546 20544 0 16:16 ? 00:00:00 postgres: checkpointer process
postgres 20547 20544 0 16:16 ? 00:00:00 postgres: writer process
postgres 20548 20544 0 16:16 ? 00:00:00 postgres: wal writer process
postgres 20549 20544 0 16:16 ? 00:00:00 postgres: autovacuum launcher process
postgres 20550 20544 0 16:16 ? 00:00:00 postgres: stats collector process
postgres 20551 20544 0 16:16 ? 00:00:00 postgres: bgworker: logical replication launcher
postgres 20552 20481 0 16:16 pts/0 00:00:00 ps -ef
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
 # 修改重启前 127.0.0.1:5432
[root@localhost pgsql_data]# netstat -tunlp | grep 5432
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 20544/postgres
tcp6 0 0 ::1:5432 :::* LISTEN 20544/postgres # 修改重启后 0.0.0.0:5432
[root@localhost pgsql_data]# netstat -tunlp | grep 5432
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 20595/postgres
tcp6 0 0 :::5432 :::* LISTEN 20595/postgres

7.连接数据库

 [root@localhost bin]# /home/pgsql/bin/psql -h 127.0.0.1 -d postgres -U postgres -p 5432
psql.bin (10.10)
Type "help" for help. 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]#

【linux】【PostgreSQL】PostgreSQL安装的更多相关文章

  1. Linux下的PostgreSQL简单安装手册

    1. 安装环境     linux版本: CentOS release 6.2 (Final)     pg版本    : postgresql-9.5.0   2. pg数据库下载地址 --http ...

  2. Linux环境PostgreSQL源码编译安装

    Linux环境PostgreSQL源码编译安装 Linux版本: Red Hat 6.4 PostgreSQL版本: postgresql-9.3.2.tar.gz 数据存放目录: /var/post ...

  3. 在linux系统下检查postgresql数据库安装,登录数据库及简单的查看数据库

    1.    检查Linux系统是否安装数据库 首先查看自己的系统是否安装了postgresql数据库命令如下: rpm -qa | grep postgresql 如果没有显示查询结果(如下图所示)说 ...

  4. Linux——CentOS 6.3下PostgreSQL 的安装与配置

    一.简介 PostgreSQL 是一种非常复杂的对象-关系型数据库管理系统(ORDBMS),也是目前功能最强大,特性最丰富和最复杂的自由软件数据库系统.有些特性甚至连商业数据库 都不具备.这个起源于伯 ...

  5. 【转】postgresql 9.4 在linux环境的安装步骤详解

    本文章来为各位介绍一篇关于postgresql 9.4 在linux环境的安装步骤详解,希望文章能够对各位新手朋友带来帮助的哦. 环境说明系统:centos 6.4 64位软件:postgresql ...

  6. Linux CentOS 7 安装PostgreSQL 9.5.17 (源码编译)

    近日需要将PostgreSQL数据库从Windows中迁移到Linux中,Linux CentOS 7 安装PostgreSQL 9.5.17 安装过程 特此记录. 安装环境: 数据库:Postgre ...

  7. CentOS 6.3下PostgreSQL 的安装与配置

    一.简介 PostgreSQL 是一种非常复杂的对象-关系型数据库管理系统(ORDBMS),也是目前功能最强大,特性最丰富和最复杂的自由软件数据库系统.有些特性甚至连商业数据库都不具备.这个起源于伯克 ...

  8. postgresql编译安装与调试(二)

    接前文postgresql编译安装与调试(一),继续说说postgresql的编译安装与调试. 上一篇已经详细说明了如何在Linux系统上编译安装postgresql,这次我们在此基础上简单讲讲如何在 ...

  9. 关于linux上postgresql的一些理解

    刚开始接触postgresql,安装后就有一个默认用户postgres,而且在启动postgresql后只能通过切换到linux的postgres用户才能登录数据库进行操作,和Mysql的登录认证居然 ...

  10. postgresql数据库安装及简单操作

    自从MySQL被Oracle收购以后,PostgreSQL逐渐成为开源关系型数据库的首选. 本文介绍PostgreSQL的安装和基本用法,供初次使用者上手.以下内容基于Debian操作系统,其他操作系 ...

随机推荐

  1. Spring中jdbcTemplate的用法实例

    一.首先配置JdbcTemplate: 要使用Jdbctemplate 对象来完成jdbc 操作.通常情况下,有三种种方式得到JdbcTemplate 对象.       第一种方式:我们可以在自己定 ...

  2. springBoot配置activeMq点对点模式消费信息以及独占模式消费如何设置

    1.在pom文件中引入对应jar包 <!--activeMQ start--> <dependency> <groupId>org.springframework. ...

  3. 翻牌动画(CocosCreator)

    推荐阅读:  我的CSDN  我的博客园  QQ群:704621321       在游戏中,有时候为了通过一种有意思的途径,让用户在一些物品中随机获取某种物品,除了前面我们提到的使用大转盘抽奖获得, ...

  4. Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths)

    Leetcode之深度优先搜索(DFS)专题-1080. 根到叶路径上的不足节点(Insufficient Nodes in Root to Leaf Paths) 这篇是DFS专题的第一篇,所以我会 ...

  5. 一个手写的Vue放大镜,复制即可使用

    一个手写的vue放大镜 组件使用less,请确保已安装loader 本组件为放大镜组件,传参列表为: width: 必传,设置放大镜的宽高(正方形),放大区域等同,放大倍数为2倍 picList:必传 ...

  6. Worker Service in ASP .NET Core

    介绍 提到 ASP.NET Core,我们多半会想到 ASP.NET MVC.ASP.NET Web API.Razor page 及 Blazor.随着 .NET Core 3.0 的推出,今天会介 ...

  7. Redis高可用架构

    前言 Redis是一个高性能的key-value数据库,现时越来越多企业与应用使用Redis作为缓存服务器.楼主是一枚JAVA后端程序员,也算是半个运维工程师了.在Linux服务器上搭建Redis,怎 ...

  8. docker进阶1-命令补充和容器卷使用

    命令补充 docker信息与帮助 docker version 和 docker info docker --help 查看所有docker命令列表 docker --help run/commit/ ...

  9. 逆序对 线段树&树状数组 (重制版)

    逆序对的定义:长度为n的数组a,求满足i<j时a[i]>a[j]条件的数对个数. 第一次接触这种问题的人可能是更先想到的是n^2去暴力数前面有几个比他大的数. int main() { i ...

  10. hdu 3265 Posters(线段树+扫描线+面积并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3265 题意:给你一张挖了洞的墙纸贴在墙上,问你总面积有多少. 挖了洞后其实就是多了几个矩形墙纸,一张墙 ...