操作系统版本:Centos 7.4
一、安装postgresql,这里的版本是postgresql-9.2.24-2
yum install postgresql postgresql-server -y
想启动postgresql,但是失败,通过journalctl -xe查看:
Mar 29 12:32:58 Centos7.4_2 systemd[1]: postgresql.service: control process exited, code=exited status=1
Mar 29 12:32:58 Centos7.4_2 postgresql-check-db-dir[61166]: "/var/lib/pgsql/data" is missing or empty.
Mar 29 12:32:58 Centos7.4_2 postgresql-check-db-dir[61166]: Use "postgresql-setup initdb" to initialize the database cluster.
Mar 29 12:32:58 Centos7.4_2 postgresql-check-db-dir[61166]: See /usr/share/doc/postgresql-9.2.24/README.rpm-dist for more information.
Mar 29 12:32:58 Centos7.4_2 systemd[1]: Failed to start PostgreSQL database server.
-- Subject: Unit postgresql.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit postgresql.service has failed.
--
-- The result is failed.
Mar 29 12:32:58 Centos7.4_2 systemd[1]: Unit postgresql.service entered failed state.
Mar 29 12:32:58 Centos7.4_2 systemd[1]: postgresql.service failed.
Mar 29 12:32:58 Centos7.4_2 polkitd[38266]: Unregistered Authentication Agent for unix-process:61160:1067078 (system bus name :1.2408, object path /org/freed
二、经查资料需要先用 postgres 用户初始化数据库
1、su - postgres
2、initdb
3、exit
[root@Centos7 ftpuser]# su - postgres
-bash-4.2$
-bash-4.2$
-bash-4.2$ initdb
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 "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".
fixing permissions on existing directory /var/lib/pgsql/data ... ok
creating subdirectories ... ok
selecting default max_connections ... 100
selecting default shared_buffers ... 32MB
creating configuration files ... ok
creating template1 database in /var/lib/pgsql/data/base/1 ... ok
initializing pg_authid ... ok
initializing dependencies ... ok
creating system views ... ok
loading system objects' descriptions ... ok
creating collations ... ok
creating conversions ... ok
creating dictionaries ... ok
setting privileges on built-in objects ... ok
creating information schema ... ok
loading PL/pgSQL server-side language ... ok
vacuuming database template1 ... ok
copying template1 to template0 ... ok
copying template1 to postgres ... 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:
postgres -D /var/lib/pgsql/data
or
pg_ctl -D /var/lib/pgsql/data -l logfile start
-bash-4.2$ exit
三、启动postgresql
systemctl start postgresql
四、创建用户和数据库
这里要注意数据库命令最后必须要有分号";"
[root@Centos7 ~]# su - postgres
Last login: Sun Mar 29 12:55:17 CST 2020 on pts/4
-bash-4.2$
-bash-4.2$ psql
psql (9.2.24)
Type "help" for help.
postgres=#
postgres=# create user pgsql with password '******';
CREATE ROLE
postgres=# create database dbtest owner pgsql;
CREATE DATABASE
给用户赋权:
postgres=# grant all on database dbtest to pgsql;
GRANT
五、配置允许远程访问PostgreSQL
PostgreSQL默认是只能数据库服务器本机访问,需要进行配置修改(经测试发现从其它机器telnet到postgresql服务器端口5432不通,但是数据库本地是有监听的)
注意有两个地方的配置文件要改:pg_hba.conf 和 postgresql.conf
1.先找到配置文件
[root@Centos7 /]# find / -name pg_hba.conf
/root/atsea-sample-shop-app/database/pg_hba.conf
/var/lib/pgsql/data/pg_hba.conf
[root@Centos7 ~]# find / -name postgresql.conf
/root/atsea-sample-shop-app/database/postgresql.conf
/var/lib/pgsql/data/postgresql.conf
/usr/lib/tmpfiles.d/postgresql.conf
2.编辑/var/lib/pgsql/data/pg_hba.conf(根据实际需求,修改ipv4或ipv6配置),这样才能从其它地址连接到数据库IP。
# IPv4 local connections:
host all all 127.0.0.1/32 trust
# IPv6 local connections:
host all all ::1/128 trust
改为:
# IPv4 local connections:
host all all 0.0.0.0/0 trust
# IPv6 local connections:
host all all ::1/128 trust
3.编辑/var/lib/pgsql/data/postgresql.conf,将listen_addresses设置为 *
# - Connection Settings -
#listen_addresses = 'localhost' # what IP address(es) to listen on;
改为(注意重启postgresql服务后才生效):
# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
4、配置从IDE去连接Postgresql,执行成功:
import psycopg2
cxn=psycopg2.connect(host='10.0.0.131',database='dbtest',user='pgsql',password='******')
#此处密码用*号代替
Process finished with exit code 0
参考文档:
1、https://techglimpse.com/solution-polkitd-postgresql-start-error/
2、https://www.jianshu.com/p/d9fecdeaa759
3、https://www.cnblogs.com/weihengblog/p/10082570.html
4、https://blog.csdn.net/u010936475/article/details/52729605
5、https://www.cnblogs.com/xiaoliu66007/p/12233081.html