首页 > 数据库 > PostgreSQL > 初识PostGresql
初识PostGresql
公司的一个项目可能需要使用PG,就学习一下,给自己留一个笔记吧:
1、添加用户
[root@localhost bin]# cat /etc/redhat-release
CentOS Linux release 7.4.1708 (Core)
#groupadd postgres
#useradd -g postgres postgres
#passwd postgres
2、安装PG 12.6
在官网下载菜单,按操作系统选择:
Linux --> Redhat/CentOS -->Version 12 --> PlatForm-->Version 7 --> X86_64
# Install the repository RPM:
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
# Install PostgreSQL:
sudo yum install -y postgresql12-server
# Optionally initialize the database and enable automatic start:
sudo /usr/pgsql-12/bin/postgresql-12-setup initdb ##初始化
sudo systemctl enable postgresql-12
sudo systemctl start postgresql-12
3、设置
[postgres@localhost ~]$ cd /var/lib/pgsql/12/data/ 初始的数据文件目录
[postgres@localhost bin]$ pwd
/usr/pgsql-12/bin 初始二进制命令和配置文件目录
##查看运行状态
[postgres@localhost bin]$ ./pg_ctl status -D /var/lib/pgsql/12/data
pg_ctl: 正在运行服务器进程(PID: 1739)
/usr/pgsql-12/bin/postgres "-D" "/var/lib/pgsql/12/data"
##停止数据库
[postgres@localhost bin]$ ./pg_ctl stop -D /var/lib/pgsql/12/data
等待服务器进程关闭 .... 完成
服务器进程已经关闭
##启动数据库
[postgres@localhost bin]$ ./pg_ctl start -D /var/lib/pgsql/12/data
等待服务器进程启动 ....2021-04-12 16:49:59.955 CST [2024] 日志: 正在启动 PostgreSQL 12.6 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2021-04-12 16:49:59.957 CST [2024] 日志: 正在监听IPv4地址"0.0.0.0",端口 5432
2021-04-12 16:49:59.957 CST [2024] 日志: 正在监听IPv6地址"::",端口 5432
2021-04-12 16:49:59.960 CST [2024] 日志: 在Unix套接字 "/var/run/postgresql/.s.PGSQL.5432"上侦听
2021-04-12 16:49:59.965 CST [2024] 日志: 在Unix套接字 "/tmp/.s.PGSQL.5432"上侦听
2021-04-12 16:49:59.971 CST [2024] 日志: 日志输出重定向到日志收集进程
2021-04-12 16:49:59.971 CST [2024] 提示: 后续的日志输出将出现在目录 "log"中.
完成
服务器进程已经启动
##配置远程访问
[postgres@localhost data]$ pwd
/var/lib/pgsql/12/data
[postgres@localhost data]$ vi pg_hba.conf
# IPv4 local connections:
host all all 127.0.0.1/32 ident
host all all 0.0.0.0/0 password
[postgres@localhost data]$ vi postgresql.conf
listen_addresses = '*' # what IP address(es) to listen on;
#listen_addresses = 'localhost' # what IP address(es) to listen on;
重新启动数据
[postgres@localhost bin]$ ./psql
psql (12.6)
输入 "help" 来获取帮助信息.
postgres-# \l
数据库列表
名称 | 拥有者 | 字元编码 | 校对规则 | Ctype | 存取权限
-----------+----------+----------+-------------+-------------+------------------
-----
mydb | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 |
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/post
gres
template1 | postgres | UTF8 | zh_CN.UTF-8 | zh_CN.UTF-8 | =c/postgres
+
| | | | | postgres=CTc/post
gres
(4 行记录)
postgres-# \c postgres
您现在已经连接到数据库 "postgres",用户 "postgres".
postgres=# ALTER USER postgres WITH PASSWORD 'welcome1';
ALTER ROLE
postgres=# \q
[postgres@localhost bin]$ ./psql -U postgres -d postgres -h 192.168.56.10 -p 5432
用户 postgres 的口令:
psql (12.6)
输入 "help" 来获取帮助信息.
postgres=# \q
Cool!! 比Oracle 安装简单太多了。。。。。。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/6126/viewspace-2767638/ ,如需转载,请注明出处,否则将追究法律责任。