Postgresql 简明教程

PostgreSQL - Environment Setup

要开始理解 PostgreSQL 基础知识,首先让我们安装 PostgreSQL。本章解释了如何在 Linux、Windows 和 Mac OS 平台上安装 PostgreSQL。

To start understanding the PostgreSQL basics, first let us install the PostgreSQL. This chapter explains about installing the PostgreSQL on Linux, Windows and Mac OS platforms.

Installing PostgreSQL on Linux/Unix

按照给定的步骤在 Linux 机器上安装 PostgreSQL。请确保在进行安装时以 root 登录。

Follow the given steps to install PostgreSQL on your Linux machine. Make sure you are logged in as root before you proceed for the installation.

  1. Pick the version number of PostgreSQL you want and, as exactly as possible, the platform you want from EnterpriseDB

  2. I downloaded postgresql-9.2.4-1-linux-x64.run for my 64 bit CentOS-6 machine. Now, let us execute it as follows −

[root@host]# chmod +x postgresql-9.2.4-1-linux-x64.run
[root@host]# ./postgresql-9.2.4-1-linux-x64.run
------------------------------------------------------------------------
Welcome to the PostgreSQL Setup Wizard.

------------------------------------------------------------------------
Please specify the directory where PostgreSQL will be installed.

Installation Directory [/opt/PostgreSQL/9.2]:
  1. Once you launch the installer, it asks you a few basic questions like location of the installation, password of the user who will use database, port number, etc. So keep all of them at their default values except password, which you can provide password as per your choice. It will install PostgreSQL at your Linux machine and will display the following message −

Please wait while Setup installs PostgreSQL on your computer.

 Installing
 0% ______________ 50% ______________ 100%
 #########################################

-----------------------------------------------------------------------
Setup has finished installing PostgreSQL on your computer.
  1. Follow the following post-installation steps to create your database −

[root@host]# su - postgres
Password:
bash-4.1$ createdb testdb
bash-4.1$ psql testdb
psql (8.4.13, server 9.2.4)

test=#
  1. You can start/restart postgres server in case it is not running using the following command −

[root@host]# service postgresql restart
Stopping postgresql service:                               [  OK  ]
Starting postgresql service:                               [  OK  ]
  1. If your installation was correct, you will have PotsgreSQL prompt test=# as shown above.

Installing PostgreSQL on Windows

按照给定的步骤在 Windows 机器上安装 PostgreSQL。请确保在安装的同时关闭第三方防病毒软件。

Follow the given steps to install PostgreSQL on your Windows machine. Make sure you have turned Third Party Antivirus off while installing.

  1. Pick the version number of PostgreSQL you want and, as exactly as possible, the platform you want from EnterpriseDB

  2. I downloaded postgresql-9.2.4-1-windows.exe for my Windows PC running in 32bit mode, so let us run postgresql-9.2.4-1-windows.exe as administrator to install PostgreSQL. Select the location where you want to install it. By default, it is installed within Program Files folder.

postgresql install dir
  1. The next step of the installation process would be to select the directory where your data would be stored. By default, it is stored under the "data" directory.

postgresql install data
  1. Next, the setup asks for password, so you can use your favorite password.

postgresql install password
  1. The next step; keep the port as default.

postgresql install port
  1. In the next step, when asked for "Locale", I selected "English, United States".

  2. It takes a while to install PostgreSQL on your system. On completion of the installation process, you will get the following screen. Uncheck the checkbox and click the Finish button.

postgresql install finish

安装过程完成后,您可以从 PostgreSQL 9.2 下的程序菜单中访问 pgAdmin III、StackBuilder 和 PostgreSQL shell。

After the installation process is completed, you can access pgAdmin III, StackBuilder and PostgreSQL shell from your Program Menu under PostgreSQL 9.2.

Installing PostgreSQL on Mac

按照给定的步骤在 Mac 机器上安装 PostgreSQL。请确保在进行安装时以 administrator 登录。

Follow the given steps to install PostgreSQL on your Mac machine. Make sure you are logged in as administrator before you proceed for the installation.

  1. Pick the latest version number of PostgreSQL for Mac OS available at EnterpriseDB

  2. I downloaded postgresql-9.2.4-1-osx.dmg for my Mac OS running with OS X version 10.8.3. Now, let us open the dmg image in finder and just double click it which will give you PostgreSQL installer in the following window −

postgresql install mac
  1. Next, click the postgres-9.2.4-1-osx icon, which will give a warning message. Accept the warning and proceed for further installation. It will ask for the administrator password as seen in the following window −

postgresql password mac

输入密码,进行安装,在此步骤后,重新启动 Mac 机器。如果您看不到以下窗口,请重新启动安装。

Enter the password, proceed for the installation, and after this step, restart your Mac machine. If you do not see the following window, start your installation once again.

postgresql startinstall mac
  1. Once you launch the installer, it asks you a few basic questions like location of the installation, password of the user who will use database, port number etc. Therefore, keep all of them at their default values except the password, which you can provide as per your choice. It will install PostgreSQL in your Mac machine in the Application folder which you can check −

postgresql application mac
  1. Now, you can launch any of the program to start with. Let us start with SQL Shell. When you launch SQL Shell, just use all the default values it displays except, enter your password, which you had selected at the time of installation. If everything goes fine, then you will be inside postgres database and a postgress# prompt will be displayed as shown below −

postgresql sqlshell mac

恭喜!!!现在,您的环境已准备好开始使用 PostgreSQL 数据库编程。

Congratulations!!! Now you have your environment ready to start with PostgreSQL database programming.