postgresql数据库设置远程登陆账户和密码

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
简介:

1.本地登陆postgresql,建库授权,设置密码

服务器本地登陆postgresql数据库(默认是不需要密码的)

postgres@localhost ~]$ psql

psql.bin (9.5.9)

Type "help" for help.

创建角色,并且给角色设置密码:

postgres=# create user testwjw with password 'Zykj@5&^%996';

CREATE ROLE

修改数据库用户和密码:

postgres=# alter user testwjw with password '558996';

ALTER ROLE


指定字符集创建数据库testdb1,并且授权给testwjw

postgres=# create database testdb1 with encoding='utf8' owner=testwjw;

CREATE DATABASE

授权:

postgres=# grant all privileges on database testdb1 to testwjw; 

GRANT

2.修改postgresql.conf文件中的端口和监听主机:

postsql默认安装后是监听本机127.0.0.1 默认端口是5432,是不能够远程登陆的,所以要修改监听主机地址,同时修改默认的端口为:36985

postgresql数据库的配置文件是:postgresql.conf,所在位置是:postgresql初始化时所指定的data数据目录下:

[postgres@localhost ~]$ ll /data/postgresql/data/postgresql.conf

-rw------- 1 postgres postgres 21305 Oct  3 11:18 /data/postgresql/data/postgresql.conf


[postgres@localhost ~]$ egrep "listen_addresses|5432"/data/postgresql/data/postgresql.conf 

listen_addresses = 'localhost'# what IP address(es) to listen on;

port = 5432# (change requires restart)

修改监听主机为*,端口为:36985

[postgres@localhost ~]$ egrep "listen_addresses|36985" /data/postgresql/data/postgresql.conf 

listen_addresses = '*'# what IP address(es) to listen on;

port = 36985# (change requires restart)

修改配置文件pg_hba.conf ,允许远程ip访问本地数据库,以及设置服务器本地登陆postgresql数据库要求输入密码才可以登陆

[postgres@localhost ~]$ egrep "60.223.153.25|127.0.0.1" /data/postgresql/data/pg_hba.conf 

host    all             all             60.223.153.25/32            trust

host    all             all             127.0.0.1/32            password

#host    replication     postgres        127.0.0.1/32            trust

允许  60.223.153.25ip访问服务器postgresql数据库

psql -Utestwjw -dpostgres -p36985 -h 127.0.0.1 这样访问数据库127.0.0.1数据库必须输入密码才可以

3.重启postgresql服务生效:

[postgres@localhost ~]$ pg_ctl -D /data/postgresql/data -l /data/postgresql/log/postgres.log restart

waiting for server to shut down....LOG:  received fast shutdown request

LOG:  aborting any active transactions

LOG:  autovacuum launcher shutting down

LOG:  shutting down

LOG:  database system is shut down

 done

server stopped

server starting

[postgres@localhost ~]$ netstat -lntup|grep postgres

(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 0.0.0.0:36985               0.0.0.0:*                   LISTEN      6472/postgres  

4.登陆数据库:

[postgres@localhost ~]$ psql

psql.bin: could not connect to server: No such file or directory

Is the server running locally and accepting

connections on Unix domain socket "/tmp/.s.PGSQL.5432"?

[postgres@localhost ~]$ psql -Utestwjw -dpostgres -p36985 -h 127.0.0.1

Password for user testwjw: 

psql.bin: fe_sendauth: no password supplied

[postgres@localhost ~]$ psql -Utestwjw -dpostgres -p36985 -h 127.0.0.1

Password for user testwjw: 

psql.bin (9.5.9)

Type "help" for help.


postgres=> \q

[postgres@localhost ~]$ psql -Utestwjw -dtestdb1 -p36985 -h 127.0.0.1

Password for user testwjw: 

psql.bin (9.5.9)

Type "help" for help.


testdb1=> select * from t;

 id | nan | note  

----+-----+-------

  1 | t   | TRUE

  2 | f   | FALSE

  3 | t   | tRue

  4 | f   | fAlse

 11 |     | null

 11 |     | NULL

  7 | t   | 't'

  8 | f   | 'f'

  9 | t   | 'yes'

 10 | f   | '0'

(10 rows)

testdb1=> 

[postgres@localhost ~]$ psql -Utestwjw -dtestdb1 -p36985 

psql.bin (9.5.9)

Type "help" for help.


testdb1=> \q

[postgres@localhost ~]$ psql -Utestwjw -dtestdb2 -p36985 

psql.bin (9.5.9)

Type "help" for help.


testdb2=> \q



 本文转自 wjw555 51CTO博客,原文链接:http://blog.51cto.com/wujianwei/1970390

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
3月前
|
存储 关系型数据库 数据库
【赵渝强老师】PostgreSQL的数据库
PostgreSQL的逻辑存储结构涵盖数据库集群、数据库、表、索引、视图等对象,每个对象有唯一的oid标识。数据库集群包含多个数据库,每个数据库又包含多个模式,模式内含表、函数等。通过特定SQL命令可查看和管理这些数据库对象。
|
26天前
|
存储 关系型数据库 数据库
华为数据库openGauss与PostgreSQL使用对比
华为openGauss数据库基于PostgreSQL内核演进,进行了多项增强。密码认证从MD5升级为SHA256;字符串存储中,char(n)、varchar(n)的n由字符改为字节,新增nvarchar2(n)表示字符,最大存储100MB;且将空字符''统一转换为null,提升了数据处理的一致性和安全性。
92 12
|
3月前
|
存储 关系型数据库 数据库
【赵渝强老师】PostgreSQL的数据库集群
PostgreSQL的逻辑存储结构涵盖了数据库集群、数据库、表、索引、视图等对象,每个对象都有唯一的oid标识。数据库集群是由单个PostgreSQL实例管理的所有数据库集合,共享同一配置和资源。集群的数据存储在一个称为数据目录的单一目录中,可通过-D选项或PGDATA环境变量指定。
|
3月前
|
关系型数据库 分布式数据库 数据库
PostgreSQL+Citus分布式数据库
PostgreSQL+Citus分布式数据库
108 15
|
3月前
|
SQL 关系型数据库 数据库
PostgreSQL性能飙升的秘密:这几个调优技巧让你的数据库查询速度翻倍!
【10月更文挑战第25天】本文介绍了几种有效提升 PostgreSQL 数据库查询效率的方法,包括索引优化、查询优化、配置优化和硬件优化。通过合理设计索引、编写高效 SQL 查询、调整配置参数和选择合适硬件,可以显著提高数据库性能。
738 1
|
关系型数据库 分布式数据库 PolarDB
《阿里云产品手册2022-2023 版》——PolarDB for PostgreSQL
《阿里云产品手册2022-2023 版》——PolarDB for PostgreSQL
398 0
|
存储 缓存 关系型数据库
|
存储 SQL 并行计算
PolarDB for PostgreSQL 开源必读手册-开源PolarDB for PostgreSQL架构介绍(中)
PolarDB for PostgreSQL 开源必读手册-开源PolarDB for PostgreSQL架构介绍
486 0
|
存储 算法 安全
PolarDB for PostgreSQL 开源必读手册-开源PolarDB for PostgreSQL架构介绍(下)
PolarDB for PostgreSQL 开源必读手册-开源PolarDB for PostgreSQL架构介绍
416 0
|
关系型数据库 分布式数据库 开发工具

热门文章

最新文章