Centos7安装postgre14
下载安装包
sudo yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
安装
sudo yum install -y postgresql14-server
可选初始化数据库并启用自动启动:
sudo /usr/pgsql-14/bin/postgresql-14-setup initdb
sudo systemctl enable postgresql-14
sudo systemctl start postgresql-14
登录postgresql
su - postgres
psql
退出postgresql
\q
exit
根用户postgres设置登录密码
ALTER USER postgres WITH PASSWORD 'postgres';
远程访问配置
postgresql 默认是不支持远程访问,需要修改2个配置文件
进入文件地址
vim /var/lib/pgsql/14/data/postgresql.conf
修改如此
LISTEN_ADDRESSES = '*' # WHAT IP ADDRESS(ES) TO LISTEN ON;
#COMMA-SEPARATED LIST OF ADDRESSES;
#DEFAULTS TO 'LOCALHOST'; USE '*' FOR ALL
#(CHANGE REQUIRES RESTART)
PORT = 5432 # (CHANGE REQUIRES RESTART)
max_connections = 100 # (change requires restart)
进入文件地址
vim /var/lib/pgsql/14/data/pg_hba.conf
全部修改为trust不用再认证输入密码
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
host all all 0.0.0.0/0 trust
查看数据库列表
\l
创建用户
create USER xxx WITH PASSWORD '123456';
创建数据库
create DATABASE xxx;
数据库归属关系
GRANT ALL PRIVILEGES ON DATABASE xxx TO xxxx;
切换数据库
#先退出
\q
#再登录数据库,下边为大写U
psql -d xxx -U xxx;