PostgreSQL 基本操作

一、安装

可以参考postgresql官网安装教程:https://www.postgresql.org/download/linux/redhat/

添加RPM:

yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

1、安装服务端:

yum install -y postgresql12-server

2、初始化数据

这里需要注意一下,初始化的时候可以指定postgresql的数据目录哦!

1)使用默认数据目录

yum安装的postgresql的默认数据目录为/var/lib/pgsql/12/data,直接初始化就行

/usr/pgsql-12/bin/postgresql-12-setup initdb    

2)使用自己规划好的数据目录

如果已经规划好别的数据目录,可以在初始化的时候用-D参数指定数据目录。

# 新建数据目录
[root@ef4b10b0a88d ~]# mkdir -p /data/postgresql/data    
   
# 授权
[root@ef4b10b0a88d ~]# chown -R postgres:postgres /data/postgresql/data

# 切换到postgres用户,初始化
[root@ef4b10b0a88d ~]# su - postgres
-bash-4.2$ /usr/pgsql-12/bin/initdb -D /data/postgresql/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 "C".
The default database encoding has accordingly been set to "SQL_ASCII".
The default text search configuration will be set to "english".

Data page checksums are disabled.

fixing permissions on existing directory /data/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... UTC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... ok

initdb: 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:

/usr/pgsql-12/bin/pg_ctl -D /data/postgresql/data -l logfile start

#修改postgresql的system.service文件
[root@ef4b10b0a88d ~]# vim /usr/lib/systemd/system/postgresql-12.service
Environment=PGDATA=/data/postgresql/data/

# 重新加载系统服务
[root@ef4b10b0a88d ~]# systemctl daemon-reload

3、启动 postgresql 服务

systemctl enable postgresql-12
systemctl start postgresql-12

postgresql会自动完成以下操作:

  • 自动生成一个linux系统用户postgres:管理数据库的系统用户

  • 数据库用户postgres:数据库超级管理员

  • 此用户的默认数据库为postgres

  • 可有修改下默认postgres数据库用户的密码:

$ su - postgres    
-bash-4.2$ psql 
psql (12.3)
Type "help" for help.

postgres=# alter role postgres with password '123456';
ALTER ROLE

二、修改配置

配置文件在数据目录中,由于我自定义了数据目录,所以是在/data/postgresql/data

cd /data/postgresql/data


vim postgresql.conf
# 修改监听的ip和端口 listen_addresses
= '*' vim pg_hba.conf
#新增下面一行 host all all
0.0.0.0/0 password # 所有的用户通过任意IP都可以使用面膜的方式登录PostgreSQL

重启数据库

systemctl restart postgresql-12

三、数据库的基础操作

1、连接数据库控制台

在本机想连接到数据库,需要切换到postgres用户下,在postgres用户下连接数据库,是不需要密码的。

切换 postgres 用户后,提示符变成-bash-4.2$,使用psql连接到数据库控制台,此时系统提示符变为postgres=#,如下:

[root@localhost data]# sudo - postgres         ##首先切换到postgres
-bash-4.2$
-bash-4.2$ psql ##输入psql psql (9.2.24, 服务器 12.5) 警告:psql 版本9.2, 服务器版本12.0. 一些psql功能可能无法工作. 输入 "help" 来获取帮助信息. postgres=#

创建用户

postgres=# create user foo with password '123456';
CREATE ROLE

需要注意:

  1. 要以英文分号结尾

  2.密码需要引号包裹

创建数据库

# 创建数据库
postgres=# create database sonar owner foo;
CREATE DATABASE

# 删除数据库
drop database testdb;

将数据库得权限,全部赋给某个用户

postgres=# grant all on database sonar to foo;
GRANT
原文地址:https://www.cnblogs.com/deny/p/14335329.html