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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

PostgreSQL連接到數(shù)據(jù)庫(kù)增刪改查

作者:慕菲煙云 更新時(shí)間: 2022-07-11 編程語(yǔ)言

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
  • 允許遠(yuǎn)程訪問(wèn)
    進(jìn)入到postgresql容器

    docker exec -it postgresql bash

    進(jìn)入postgresql配置文件

     cd /var/lib/postgresql/data/

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

    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

說(shuō)明:

-h 主機(jī)名
-p 端口號(hào)
-U 用戶名
-d 數(shù)據(jù)庫(kù)

3:常用命令:

  • 查看所有的數(shù)據(jù)庫(kù)列表
    \l
  • 使用?\c + 數(shù)據(jù)庫(kù)名?來(lái)進(jìn)入數(shù)據(jù)庫(kù)
    \c test
  • 查詢所有表列表,其中database為數(shù)據(jù)庫(kù)名稱
    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 類(lèi)型, CASE WHEN A.attnotnull='f' THEN '否' ELSE '是' END AS 是否必填, a.attnum 序號(hào) 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:增刪改查

  • 創(chuàng)建數(shù)據(jù)庫(kù)
    create database test;

  • 創(chuàng)建表student

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

  • 插入數(shù)據(jù)
    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);
  • 查詢數(shù)據(jù)
    select  * from  student
    select  * from  student where id=1
  • 修改數(shù)據(jù)
    update student set hgitht =8.8
    update student set hgitht =8.7 where id=2
  • 刪除數(shù)據(jù)
    delete  from student where  id=2

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

欄目分類(lèi)
最近更新