百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

CentOS7安装使用PostgreSQL数据库

toyiye 2024-07-15 01:14 11 浏览 0 评论

前言


今年个人搭建项目需要使用PostgreSQL12.2数据库,在这里分享下安装过程,有需要的童鞋可以参考下。


PostgreSQL是啥


PostgreSQL是自由的对象-关系型数据库服务器,在灵活的BSD风格许可证下发行。

更多知识,可以搜索:



下面将开始介绍从下载、安装到使用的说明。


1.下载


下载页面:https://www.postgresql.org/ftp/source/

$ wget https://ftp.postgresql.org/pub/source/v12.2/postgresql-12.2.tar.gz


2.操作系统Root用户安装依赖库

# yum install readline-devel
# yum install zlib-devel


3.普通用户编译

$ tar zxf postgresql-12.2.tar.gz 
$ cd postgresql-12.2
$ mkdir -p ~/3rd/postgresql-12.2
$ ./configure --prefix=/home/testerzhang/3rd/postgresql-12.2
$ make
$ make install



4.环境变量

  • 编辑环境变量
$ vim ~/.bash_profile

export PGHOME=$HOME/3rd/postgresql-12.2
export PGDATA=$PGHOME/data

export PATH=$PGHOME/bin:$PATH
  • 生效环境变量
$ source ~/.bash_profile
  • 验证
$ which psql
~/3rd/postgresql-12.2/bin/psql

$ which initdb
~/3rd/postgresql-12.2/bin/initdb


5.初始化

$ initdb
initdb: error: no data directory specified
You must identify the directory where the data for this database system
will reside.  Do this with either the invocation option -D or the
environment variable PGDATA.



如果没有配置环境变量PGDATA,就会报上面这个错,也可以指定[-D, --pgdata=]DATADIR location for this database cluster解决问题。

我们上面已经配置,直接执行就可以了。

$ initdb 
The files belonging to this database system will be owned by user "testerzhang".
This user must also own the server process.

The database cluster will be initialized with locale "zh_CN.UTF-8".
The default database encoding has accordingly been set to "UTF8".
initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8"
The default text search configuration will be set to "simple".

Data page checksums are disabled.

creating directory /home/testerzhang/3rd/postgresql-12.2/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 ... Asia/Shanghai
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:

    pg_ctl -D /home/testerzhang/3rd/postgresql-12.2/data -l logfile start

看到上面信息代表成功了。


6.访问控制配置文件pg_hba.conf


一般配置文件目录在data目录,具体看下面例子:

$ pwd
/home/testerzhang/3rd/postgresql-12.2

$ find . -name pg_hba.conf
./data/pg_hba.conf


下面开始编辑配置文件

$ vim ./data/pg_hba.conf

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
#host    all             all             127.0.0.1/32            trust
host    all             all             0.0.0.0/0               md5
# 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

这里注释原来的记录加入了:修改为0.0.0.0/0,加密方式改为md5,就表示需要密码访问

host    all             all             0.0.0.0/0               md5


7.主配置文件postgresql.conf

listen_addresses修改为监听整个网络,请根据实际修改。

$ vim ./data/postgresql.conf

#listen_addresses = 'localhost'         # what IP address(es) to listen on;
                                        # comma-separated list of addresses;
                                        # defaults to 'localhost'; use '*' for all
                                        # (change requires restart)
listen_addresses = '*'   


8.启动

-l表示日志文件目录,通常需要指定,所以我们在/usr/local/postgresql根目录下再创建一个log目录用来存放日志文件(注意别忘记赋予可写的权限)。

$ pg_ctl -D /home/testerzhang/3rd/postgresql-12.2/data -l logfile start


9.连接PostgreSQL

创建当前系统普通用户的数据库:

$ createdb testerzhang

连接数据库,默认连接跟系统用户名一样的数据库。

$ psql 
psql (12.2)
Type "help" for help.

testerzhang=# 

testerzhang-# \q

如果需要指定连接到某个数据库(test_db),可以用psql -d test_db


10.修改用户名的密码

testerzhang=# select * from pg_user;
 usename | usesysid | usecreatedb | usesuper | userepl | usebypassrls |  passwd  | valuntil | useconfig 
---------+----------+-------------+----------+---------+--------------+----------+----------+-----------
 testerzhang  |       10 | t           | t        | t       | t            | ******** |          | 
(1 row)

修改密码

testerzhang=# alter user testerzhang password 'qwerty';
ALTER ROLE


11.关闭服务

$ pg_ctl stop


12.常用命令

  • 查看所有数据库
\l
  • 切换数据库: \c 参数为数据库名
\c database
  • 切换用户: \c 参数为用户名
\c username
  • 查看所有表
\dt
  • \d 表名 —— 得到表结构
\d table


好了,今天就分享到这里了,你Get到了吗?




我是testerzhang,喜欢本文的童鞋,可以关注我+收藏,不明白的地方也可以评论留言。


相关推荐

「2022 年」崔庆才 Python3 爬虫教程 - 代理的使用方法

前面我们介绍了多种请求库,如urllib、requests、Selenium、Playwright等用法,但是没有统一梳理代理的设置方法,本节我们来针对这些库来梳理下代理的设置方法。1.准备工作...

Python 3 基础教程 - 函数(python基础函数大全)

函数是一组有组织的、可重用的代码,用于执行单个相关操作。函数为应用程序提供更好的模块化和高度的代码重用。Python提供了许多内置函数,如print()等。也可以创建自己的函数。这些函数称为用户...

Python3.7.4图文安装教程(python3.7详细安装教程)

Python更新的很快,一转眼Python2已经过时了,本文为大家详细说明Python最新版本3.7.4的安装过程,跟着步骤一步一步操作,轻松搞定安装。没有软件可以关注我头条私信我1、下载好后是一个压...

非程序员的其他从业者,三天可入门Python编程,附教程与相应工具

这是一种应用十分广泛的编程语言Python,它打破了只有程序员才能编程的“戒律”,尤其是近年来国家予以Python编程的支持,让这门语言几乎应用到各种工作中。那么对于并不是职业程序员的人,该如何才能快...

008 - 匿名函数lambda-python3-cookbook中文教程

有名函数通过def来定义有一个有名字的函数。defmyfun():return1,2,3...

花了3万多买的python教程全套,现在分享给大家(python全栈)

花了3万多买的Python教程全套,现在分享给大家(Python全栈)文末惊喜记得看完哦。...

花来3万多买的python教程全套,现在分享给大家(python全栈)

花了3万多买的Python教程全套,现在分享给大家(Python全栈)文末惊喜记得看完哦。...

Python3最新版安装教程(Windows)(python3.7.0安装教程win10)

接下来给大家讲解一下python最新安装包的安装教程。·首先大家可以去这里搜索一下我的笔记,大概讲了一下,然后找到它的官网,下载的是windows,可以看一下最新的版本。·选择64位,点击下载就即可了...

笨办法学python3》再笨的人都能学会python,附PDF,拿走不谢

《笨办法学python3》这本书的最终目标是让你起步python编程,虽然说是用“笨办法”学习写程序,但是其实并不是这样的。所谓的“笨办法”就是指这本书的教学方式,也就是“指令式”的教学,在这个过程中...

python3 (1)(python312)

Python3Introduction:LearnthebasicsofPython3programming,withitskeyfeatures,andprovideyo...

Python3 教程-- 3、解释器(python3菜鸟教程官网)

Python3解释器Python解释器Linux/Unix的系统上,Python解释器通常被安装在/usr/local/bin/python3.4这样的有效路径(目录)里。我们可以将路径/us...

《笨办法学python3》再笨的人都能学会python,附PDF,拿走不谢

《笨办法学python3》这本书的最终目标是让你起步python编程,虽然说是用“笨办法”学习写程序,但是其实并不是这样的。所谓的“笨办法”就是指这本书的教学方式,也就是“指令式”的教学,在这个过程中...

入门经典!《Python 3程序开发指南》python学习教程赠送!

《Python3程序开发指南》(????)??嗨!你们的小可爱又来辣,小编自学python时用到的视频学习教程分享给大家~都是非常系统性、非常详细的教程哦,希望能帮助到你!转发文章+私信小编“资料”...

Python3.7最新安装教程,一看就会

一、博主自言随着人工智能的快速发展,python语言越来越受大家的欢迎,博主前段时间先自学了一次,这次再次巩固,顺便分享给大家我遇到的坑。帮助大家学习的时候少走弯路。希望会对大家有所帮助,欢迎留言...

# Python 3 # Python 3 教程(#python3.10教程)

Python3教程Python的3.0版本,常被称为Python3000,或简称Py3k。相对于Python的早期版本,这是一个较大的升级。为了不带入过多的累赘,Python3....

取消回复欢迎 发表评论:

请填写验证码