cent os 安裝使用postgresql

下載

yum install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel-7-x86_64/pgdg-redhat11-11-2.noarch.rpm -y

安裝

yum -y install postgresql11 postgresql11-server postgresql11-libs

初始化數據庫

/usr/pgsql-11/bin/postgresql-11-setup initdb

設置開機自啓動PostgreSQL和啓動服務

systemctl enable postgresql-11
systemctl start postgresql-11
systemctl status postgresql-11

登錄數據庫,這裏切換賬號postgres

su - postgres
psql

Navicat連接PostgreSQL

這裏要修改配置文件postgresql.conf

find / -name postgresql.conf
vi /var/lib/pgsql/11/data/postgresql.conf
找到listen_address那裏,解開註釋並修改引號內localhost的值爲*
listen_address="*"

保存並退出,重啓postgresql服務

systemctl restart postgresql-11

修改遠程連接pg_hba.conf

find / -name pg_hba.conf
vi /var/lib/pgsql/11/data/pg_hba.conf

在文件末尾加上,如果不加上遠程連接PostgreSQL會出現no pg_hba.conf…的錯誤

host all all 0.0.0.0/0 trust
在navicat連接,如果不修改localhost爲*,navicat連接會提示錯誤“Connection Refuse”

我在這裏修改了postgres用戶的密碼,步驟如下:

  • 切換用戶後進入psql

su - postgres
psql

  • 修改密碼

alter user postgres password ‘密碼’

數據庫基本操作

  • 新建數據庫
    createdb mydb

  • 刪除數據庫
    dropdb mydb

  • 訪問數據庫 普通用戶-命令行顯示mydb=> 超級用戶-命令行顯示mydb=#
    psql mydb

常用命令

SELECT version();
SELECT current_date;

  • psql程序有許多不是 SQL 命令的內部命令。它們以反斜槓字符“\”開頭。例如,您可以通過 typing 獲取各種 PostgreSQL SQL 命令語法的幫助:
    查看幫助 \h
    退出連接 \q
  • 創建表
    CREATE TABLE products (
    product_no integer,
    name text,
    price numeric
    );
  • 插入數據
    INSERT INTO products (product_no, name, price) VALUES (1, ‘Cheese’, 9.99);
  • 更新數據
    UPDATE products SET price = 10 WHERE price = 5;
  • 刪除數據
    DELETE FROM products WHERE price = 10;
  • 查詢數據
    SELECT * from products;

參考文檔
https://www.docs4dev.com/docs/zh/postgre-sql/10.7/reference/preface.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章