PostgreSQL/Download and Installation
< PostgreSQLBefore you download PostgreSQL you must make some crucial decisions. First, there are two principle ways to get the system running: either you pick up the complete source code or the prebuild binaries. If you take the source code you must compile them with a C compiler (at least C89-compliant, in most cases people use GCC) [1] to the binary format of your computer. If you get the binaries directly the compilation step is superfluous. Next, you must know for which operating system you need the software. PostgreSQL supports a lot of UNIX-based systems (including MAC OS X) as well as Windows - both in 32- and 64-bit versions.
Download
After you have made the above decisions you can download the source code and/or the binaries from this page and its subpages. For some operating systems you will find a graphical installer which leads you through the subsequent installation steps. For the same or other operating systems not only the PostgreSQL DBMS will be downloaded but also the DBA tool pgAdmin
, which helps you doing your daily work thru its graphical interface.
There are different versions available: the actual release, old releases and the upcomming release.
Installation
Installation steps vary depending on the choosen operating system. In the simplest case the above mentioned graphical installer hides the details. The PostgreSQL wiki and documentation leads you thru all necessary steps for your operating system.
If you install from source code, details are explained for Unix systems and Windows.
After a successful installation you will have
- The PostgreSQL binaries on your disc.
- A first database cluster called 'main' on your disc. The database cluster consists of an empty database called 'postgres' (plus two template databases) and an user/role called 'postgres' as well.
- A set of programs (Unix) repectively a service (Windows) running on your computer. These programs/service handle the database cluster as an entire.
First steps
You can create your tables, views, functions etc. in this database 'postgres' and work with the user 'postgres'. But this approach is not recommended. The user 'postgres' has very high privileges by default and the database 'postgres' is sometimes used by tools and third party programs as a container for temporary data. You are encouraged to define your own database, one user who acts as the owner of the database and some application users.
As a first step start psql with user 'postgres' and create your own users. Please notice, that 'users' respective 'roles' are global objects which are known by all databases within the cluster, not only within a certain database. But users/roles have specific rights within each database.
$ psql postgres=# postgres=# -- the future owner of the new database shall be 'finance_master' with DDL and DML rights postgres=# CREATE ROLE finance_master; CREATE ROLE postgres=# ALTER ROLE finance_master WITH NOSUPERUSER INHERIT CREATEROLE CREATEDB LOGIN NOREPLICATION ENCRYPTED PASSWORD 'xxx'; ALTER ROLE postgres=# -- one user for read/write and one for read-only access (no DDL rights) postgres=# CREATE ROLE rw_user; CREATE ROLE postgres=# ALTER ROLE rw_user WITH NOSUPERUSER INHERIT NOCREATEROLE NOCREATEDB LOGIN NOREPLICATION ENCRYPTED PASSWORD 'xxx'; ALTER ROLE postgres=# CREATE ROLE ro_user; CREATE ROLE postgres=# ALTER ROLE ro_user WITH NOSUPERUSER INHERIT NOCREATEROLE NOCREATEDB LOGIN NOREPLICATION ENCRYPTED PASSWORD 'xxx'; ALTER ROLE postgres=#
Next, create a new database 'finance_db'. You can do this as user 'postgres' or as the previously created 'finance_master'.
postgres=# postgres=# CREATE DATABASE finance_db postgres=# WITH OWNER = finance_master postgres=# ENCODING = 'UTF8' postgres=# LC_COLLATE = 'en_US.UTF-8' postgres=# LC_CTYPE = 'en_US.UTF-8'; CREATE DATABASE postgres=#
As the last step you have to delegate the intended rights to the users/roles. This is a little tricky because PostgreSQL uses an elaborated role system where every role inherits rights from the implicit 'public' role.
postgres=# postgres=# \connect finance_db finance_db=# -- revoke schema creation from role 'public' because all roles inherit her rights from 'public' finance_db=# REVOKE CREATE ON DATABASE finance_db FROM public; REVOKE finance_db=# -- same: revoke table creation finance_db=# REVOKE CREATE ON SCHEMA public FROM public; REVOKE finance_db=# -- grant only DML rights to 'rw_user', no DDL rights like 'CREATE TABLE' finance_db=# GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO rw_user; GRANT finance_db=# GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO rw_user; GRANT finance_db=# -- grant read rights to the read-only user finance_db=# GRANT SELECT ON ALL TABLES IN SCHEMA public TO ro_user; GRANT postgres=#
References
- ↑ Requirements for Compilation