日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

PostgreSQL連接到數據庫增刪改查

作者:慕菲煙云 更新時間: 2022-07-11 編程語言

1:docker安裝PostgreSQL

docker run --name postgresql -v /my/own/datadir:/var/lib/postgresql/data -e POSTGRES_PASSWORD=123456 -d -p 5432:5432 postgres:14.1
  • 允許遠程訪問
    進入到postgresql容器

    docker exec -it postgresql bash

    進入postgresql配置文件

     cd /var/lib/postgresql/data/

    修改配置文件pg_hba.conf,最后一行新增如下內容

    host    all             all             0.0.0.0/0               trust

    重啟postgresql?

    docker restart postgresql 

2:連接到psotgresql:

psql -h 127.0.0.1 -p 5432 -U usename -d database

說明:

-h 主機名
-p 端口號
-U 用戶名
-d 數據庫

3:常用命令:

  • 查看所有的數據庫列表
    \l
  • 使用?\c + 數據庫名?來進入數據庫
    \c test
  • 查詢所有表列表,其中database為數據庫名稱
    select * from pg_tables where tableowner='database';
  • 查詢指定表的字段信息
    SELECT c.relname 表名稱, A.attname AS 字段名稱, col_description(A.attrelid,A.attnum) AS 注釋, format_type ( A.atttypid, A.atttypmod ) AS 類型, CASE WHEN A.attnotnull='f' THEN '否' ELSE '是' END AS 是否必填, a.attnum 序號 FROM pg_class AS c, pg_attribute AS a WHERE A.attrelid = C.oid AND A.attnum > 0 and c.relname='tablename' ORDER BY c.relname,a.attnum;
  • 退出連接
    \q

4:增刪改查

  • 創建數據庫
    create database test;

  • 創建表student

    create table student(
    ? id int,
    ? name varchar(32),
    ? birthday date,
    ? hgitht numeric(5,2)
    );

  • 插入數據
    insert into student values (1,'dym','2022-02-03 11:11:11',9.9);
    insert into student values (2,'張三','2022-02-13 11:11:11',9.3);
  • 查詢數據
    select  * from  student
    select  * from  student where id=1
  • 修改數據
    update student set hgitht =8.8
    update student set hgitht =8.7 where id=2
  • 刪除數據
    delete  from student where  id=2

原文鏈接:https://blog.csdn.net/qq_36793589/article/details/122669595

欄目分類
最近更新