postgresql 安装_CentOS7安装使用PostgreSQL数据库
前言
今年個人搭建項目需要使用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.gz2.操作系統Root用戶安裝依賴庫
# yum install readline-devel# yum install zlib-devel3.普通用戶編譯
$ 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安裝成功,OK
4.環境變量
- 編輯環境變量
- 生效環境變量
- 驗證
5.初始化
$ initdbinitdb: error: no data directory specifiedYou must identify the directory where the data for this database systemwill reside. Do this with either the invocation option -D or theenvironment 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 ... okcreating subdirectories ... okselecting dynamic shared memory implementation ... posixselecting default max_connections ... 100selecting default shared_buffers ... 128MBselecting default time zone ... Asia/Shanghaicreating configuration files ... okrunning bootstrap script ... okperforming post-bootstrap initialization ... oksyncing data to disk ... okinitdb: warning: enabling "trust" authentication for local connectionsYou 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 onlylocal all all trust# IPv4 local connections:#host all all 127.0.0.1/32 trusthost 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 trusthost replication all 127.0.0.1/32 trust這里注釋原來的記錄加入了:修改為0.0.0.0/0,加密方式改為md5,就表示需要密碼訪問
host all all 0.0.0.0/0 md57.主配置文件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 start9.連接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 ROLE11.關閉服務
$ pg_ctl stop12.常用命令
- 查看所有數據庫
- 切換數據庫: c 參數為數據庫名
- 切換用戶: c 參數為用戶名
- 查看所有表
- d 表名 —— 得到表結構
好了,今天就分享到這里了,你Get到了嗎?
我是testerzhang,喜歡本文的童鞋,可以關注我+收藏,不明白的地方也可以評論留言。
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的postgresql 安装_CentOS7安装使用PostgreSQL数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python注释以符号什么开始_pyth
- 下一篇: 深度学习每层的通道数如何计算_模型通道剪