linux下安装postgresql数据库(yum方式)

注:使用yum方式安装有默认路径在/usr/下


查看linux服务器是什么环境
cat /etc/redhat-realese
uname -a

# postgresql install Centos7
# with Yum install

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

yum install postgresql10 postgresql10-server postgresql-contrib postgresql-devel -y

# For RHEL / CentOS / SL / OL 7 or Fedora 27 and later derived distributions:
# initdb
# 初始化数据库
/usr/pgsql-10/bin/postgresql-10-setup initdb

# 启动数据库的启动服务文件 
ls /usr/lib/systemd/system/postgresql-10.service

# 设置开机启动
 systemctl enable postgresql-10.service 

# 启动数据库(root用户)
 systemctl start postgresql-10.service 
 
# 要切换到postgres用户才能登陆数据库
su - postgres

# 登陆数据库
-bash-4.2$ psql
psql (10.5)
Type "help" for help.

# 查看数据库
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(3 rows)

# 寻求帮助
postgres=# \?

# 退出数据库
postgres=# \q

# 创建数据库
-bash-4.2$ createdb paojiao

# 查看帮助
-bash-4.2$  createdb --help

# 登陆数据库
-bash-4.2$ psql

# 查看刚创建的库
postgres=# \l
                                  List of databases
   Name    |  Owner   | Encoding |   Collate   |    Ctype    |   Access privileges   
-----------+----------+----------+-------------+-------------+-----------------------
 paojiao   | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 postgres  | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | 
 template0 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
 template1 | postgres | UTF8     | en_US.UTF-8 | en_US.UTF-8 | =c/postgres          +
           |          |          |             |             | postgres=CTc/postgres
(4 rows)

# 删除数据库并查看帮助
-bash-4.2$ dropdb --help
-bash-4.2$ createuser --help

# 登陆刚创建的数据库
-bash-4.2$ psql paojiao
-bash-4.2$ psql --help

# 查询版本和日期
paojiao=# select version();
paojiao=# select current_date;
paojiao=# \h
paojiao=# \h show
paojiao=# \h alter database;

# 查看表
paojiao=# \dt
paojiao=# \d  # 按tab 提示
paojiao=# \?
# 调用系统命令行命令
paojiao-# \! ps aux|grep pg
paojiao-# \! ifconfig

# 查数据  SELECT 
paojiao=# SELECT * FROM weather;
paojiao=# SELECT city, (temp_hi+temp_lo)/2 AS temp_avg, date FROM weather;
paojiao=# SELECT * FROM weather WHERE city = 'San Francisco' AND prcp > 0.0;
paojiao=# SELECT * FROM weather ORDER BY city;
paojiao=# select distinct city from weather;
paojiao=# select distinct city from weather ORDER BY city;
paojiao=# SELECT * FROM weather, cities WHERE city = name;
paojiao=# SELECT city, temp_lo, temp_hi, prcp, date, location FROM weather, cities WHERE city = name;
paojiao=# SELECT weather.city, weather.temp_lo, weather.temp_hi,weather.prcp, weather.date, cities.location FROM weather, cities WHERE cities.name = weather.city;
paojiao=# SELECT * FROM weather INNER JOIN cities ON (weather.city = cities.name);
paojiao=# SELECT * FROM weather LEFT OUTER JOIN cities ON (weather.city = cities.name);
paojiao=# SELECT W1.city, W1.temp_lo AS low, W1.temp_hi AS high,W2.city, W2.temp_lo AS low, W2.temp_hi AS high FROM weather W1, weather W2 WHERE W1.temp_lo < W2.temp_lo AND W1.temp_hi > W2.temp_hi;
paojiao=# SELECT * FROM weather w, cities c WHERE w.city = c.name;
paojiao=# SELECT max(temp_lo) FROM weather;
paojiao=# SELECT city FROM weather WHERE temp_lo = (SELECT max(temp_lo) FROM weather);
paojiao=# SELECT city, max(temp_lo) FROM weather GROUP BY city;
SELECT city, max(temp_lo)
    FROM weather
    GROUP BY city
    HAVING max(temp_lo) < 40;
	
	
	
SELECT city, max(temp_lo)
    FROM weather
    WHERE city LIKE 'S%' 
    GROUP BY city
    HAVING max(temp_lo) < 40;
	
	
##  更新数据   2.8. Updates

UPDATE weather
    SET temp_hi = temp_hi - 2,  temp_lo = temp_lo - 2
    WHERE date > '1994-11-28';
SELECT * FROM weather;


###############    Chapter 3. Advanced Features
###  3.2. Views
CREATE VIEW myview AS
    SELECT city, temp_lo, temp_hi, prcp, date, location
        FROM weather, cities
        WHERE city = name;

SELECT * FROM myview;

##########   3.3. Foreign Keys

修改postgres用户的密码为postgres:

ALTER USER postgres WITH PASSWORD 'postgres';

授权远程登录:

  • 修改postgresql.conf 文件,如果想让PostgreSQL 监听整个网络的话,将listen_addresses 前的#去掉,并将 listen_addresses = ‘localhost’ 改成 listen_addresses = ‘*’
# vi /var/lib/pgsql/9.2/data/postgresql.conf
  • 修改客户端认证配置文件pg_hba.conf,在IPV4下面添加一条记录”host all all 10.1.5.0/24 md5”,其中10.1.5.0代表网段’10.1.5.*’, 24是子网掩码,代表10.1.5.0-10.1.5.255, md5代表使用md5加密
# vi /var/lib/pgsql/9.2/data/pg_hba.conf

ctrl+d  按两次即可退出该数据库.

  • 远程连接测试,默认端口是5432 - 成功

参考:

https://www.postgresql.org/download/linux/redhat/  pg官网

http://www.postgres.cn/docs/9.5/index.html 中文官方文档

http://ju.outofmemory.cn/entry/217355?tdsourcetag=s_pcqq_aiomsg

猜你喜欢

转载自blog.csdn.net/mqingo/article/details/83856596
今日推荐