Linux中PostgreSQL和PostGIS的安装和使用方法


本文摘自PHP中文网,作者小云云,侵删。

本文主要和大家介绍Linux中PostgreSQL和PostGIS的安装和使用,并把需要注意点做了分析和解释,需要的朋友学习下,希望能帮助到大家。

安装 PostgreSQL 和 PostGIS

PostgreSQL 和 PostGIS 已经是热门的开源工程,已经收录在各大 Linux 发行版的 yum 或 apt 包中。Ubuntu 为例,安装以下包即可:


1

$ sudo apt-get install postgresql-client postgresql postgis -y

RedHat 系列则请安装:


1

$ sudo yum install postgresql-server postgresql postgis

初次安装后,默认生成一个名为 postgres 的数据库和一个名为 postgres 的数据库用户。这里需要注意的是,同时还生成了一个名为 postgres 的 Linux 系统用户。我们以后在操作 PostgreSQL 的时候都应该在这个新创建的 postgres 用户中进行。

PostgreSQL 配置

如果是从源码安装

不建议从源码安装,我曾经试过从源码安装,实在是太麻烦了,而且各种 make install 容易出错。最后我还是用 rpm 安装了。不过既然花了些时间研究并且我成功安装过,所以还是记录一下吧――不过,可能有错漏,所以读者如果要从源码安装的话,请做好回滚的准备。

如果使用的是通过 source 编译并且 make install 安装,那么这一节是需要额外配置的。

貌似 CentOS 系列的安装也需要……

默认的 make install 之后,PostgreSQL 安装目录在:/usr/local/pgsql/

首先根据这个链接的参考,需要配置环境变量


1

$ set $PGDATA = "/usr/local/pgsql/database"

但是执行了 pg_ctl start 之后,会出现错误:


1

pg_ctl: directory "/usr/local/pgsql/database" is not a database cluster directory

这样的话,就需要参照 PostGreSQL 官方文档的步骤创建真正的 database:<br/>

PostgreSQL: Documentation: 9.1: Creating a Database Cluster

首先创建一个用户账户,名叫 postgres


1

2

$ usradd postgres

$ sudo chown postgres /usr/local/pgsql/database

然后进入这个账户,创建 database


1

2

$ sudo su postgres

$ initdb -D /usr/local/pgsql/database/

此时 shell 会输出:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

The files belonging to this database system will be owned by user "postgres".

This user must also own the server process.

 

The database cluster will be initialized with locale "C".

The default database encoding has accordingly been set to "SQL_ASCII".

The default text search configuration will be set to "english".

 

Data page checksums are disabled.

 

fixing permissions on existing directory /usr/local/pgsql/database ... ok

creating subdirectories ... ok

selecting default max_connections ... 100

selecting default shared_buffers ... 128MB

selecting dynamic shared memory implementation ... posix

creating configuration files ... ok

creating template1 database in /usr/local/pgsql/database/base/1 ... ok

initializing pg_authid ... ok

initializing dependencies ... ok

creating system views ... ok

loading system objects' descriptions ... ok

creating collations ... ok

creating conversions ... ok

creating dictionaries ... ok

setting privileges on built-in objects ... ok

creating information schema ... ok

loading PL/pgSQL server-side language ... ok

vacuuming database template1 ... ok

copying template1 to template0 ... ok

copying template1 to postgres ... ok

syncing data to disk ... ok

 

WARNING: enabling "trust" authentication for local connections

You 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 /usr/local/pgsql/database/ -l logfile start

恭喜你,接下来就可以启动 PostgreSQL 了:


1

pg_ctl -D /usr/local/pgsql/database/ -l /usr/local/pgsql/database/psql.log start

PostgreSQL 安装好后

进入 postgres 账户,并且进入 PostgreSQL 控制台:


1

2

$ sudo su postgres

$ psql

这时相当于系统用户 postgres 以同名数据库用户的身份,登录数据库,否则我们每次执行 psql 的时候都要在参数中指定用户,容易忘。

阅读剩余部分

相关阅读 >>

linux内核如何编译

linux怎么开启和关闭防火墙

linux下9种优秀的代码比对工具【推荐】

linux 临时和永久关闭 selinux

linux find命令怎么用

更换一台 ecs 实例的系统盘或者操作系统应该怎么操作

linux命令rmdir的用法总结

如何使用modifynetworkinterfaceattribute修改一个弹性网卡(eni)属性

linux文件在哪

linux查看用户的方法

更多相关阅读请进入《PostgreSQL》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...