个人工具

“UbuntuHelp:PostgreSQL”的版本间的差异

来自Ubuntu中文

跳转至: 导航, 搜索
 
(未显示同一用户的7个中间版本)
第2行: 第2行:
 
{{Languages|UbuntuHelp:PostgreSQL}}
 
{{Languages|UbuntuHelp:PostgreSQL}}
 
=== Introduction ===
 
=== Introduction ===
[[UbuntuHelp:PostgreSQL|PostgreSQL]] is a powerful object-relational database management system, provided under a flexible BSD-style license.<sup>[1]</sup> [[UbuntuHelp:PostgreSQL|PostgreSQL]] contains many advanced features, is very fast and standards compliant.
+
PostgreSQL is a powerful object-relational database management system, provided under a flexible BSD-style license.<sup>[1]</sup> PostgreSQL contains many advanced features, is very fast and standards compliant.
[[UbuntuHelp:PostgreSQL|PostgreSQL]] has bindings for many programming languages such as C, C++, [http://python.org/ Python], Java, PHP, Ruby... It can be used to power anything from simple web applications to massive databases with millions of records.
+
PostgreSQL has bindings for many programming languages such as C, C++, [http://python.org/ Python], Java, PHP, Ruby... It can be used to power anything from simple web applications to massive databases with millions of records.
 
=== Installation ===
 
=== Installation ===
 
==== Dapper ====
 
==== Dapper ====
To install [[UbuntuHelp:PostgreSQL|PostgreSQL]] 8.1 you may use the command line and type :
+
To install '''PostgreSQL 8.1''' you may use the command line and type:
 
<pre><nowiki>
 
<pre><nowiki>
sudo apt-get install postgresql-8.1
+
sudo apt-get install postgresql-8.1
 
</nowiki></pre>
 
</nowiki></pre>
pgAdmin III is a handy GUI for [[UbuntuHelp:PostgreSQL|PostgreSQL]], it is essential to beginners. To install type at the command line :
+
==== Hardy ====
 +
Install the latest '''Postgresql 8.3''' from the command line with the following command:
 
<pre><nowiki>
 
<pre><nowiki>
sudo apt-get install pgadmin3
+
sudo apt-get install postgresql
 +
</nowiki></pre>
 +
==== Karmic ====
 +
To install '''Postgresql 8.4''' you may use the command line and type:
 +
<pre><nowiki>
 +
sudo apt-get install postgresql
 +
</nowiki></pre>
 +
==== Lucid ====
 +
To install '''Postgresql 8.4''' you may use the command line and type:
 +
<pre><nowiki>
 +
sudo apt-get install postgresql
 +
</nowiki></pre>
 +
==== Administration ====
 +
pgAdmin III is a handy GUI for PostgreSQL, it is essential to beginners. To install it, type at the command line:
 +
<pre><nowiki>
 +
sudo apt-get install pgadmin3
 
</nowiki></pre>
 
</nowiki></pre>
 
You may also use the Synaptic package manager from the System>Administration menu to install these packages.
 
You may also use the Synaptic package manager from the System>Administration menu to install these packages.
==== Gutsy ====
 
To install '''Postgresql-8.2''' using your favorite package manager install the '''postgresql''' package.
 
 
=== Basic Server Setup ===
 
=== Basic Server Setup ===
==== Set password ====
+
To start off, we need to change the PostgreSQL postgres user password; we will not be able to access the server otherwise. As the “postgres” Linux user, we will execute the psql command.
To start off, we need to change the [[UbuntuHelp:PostgreSQL|PostgreSQL]] postgres user password, we will not be able to access the server otherwise. As the “postgres” Linux user, we will execute the psql command, in a terminal type (note, since Gusty this fails and needs an update):
+
In a terminal, type:
 +
==== Dapper ====
 
<pre><nowiki>
 
<pre><nowiki>
 
sudo -u postgres psql template1
 
sudo -u postgres psql template1
 
</nowiki></pre>
 
</nowiki></pre>
Then at the new prompt, type these two commands, replacing <***password***> with the new password (keep this safe ;) ) :
+
==== Karmic and Lucid ====
 
<pre><nowiki>
 
<pre><nowiki>
ALTER USER postgres WITH ENCRYPTED PASSWORD ' <***password***> ';
+
sudo -u postgres psql postgres
\q
+
 
</nowiki></pre>
 
</nowiki></pre>
===== Gutsy =====
+
Set a password for the "postgres" database role using the command:
I used this to change the password for the default superuser (postgres) under gusty.
+
 
<pre><nowiki>
 
<pre><nowiki>
sudo -u postgres psql -c "ALTER USER postgres WITH PASSWORD '<***password***>'"
+
\password postgres
 
</nowiki></pre>
 
</nowiki></pre>
 +
and give your password when prompted. The password text will be hidden from the console for security purposes.
 +
Type Control+D to exit the posgreSQL prompt.
 
==== Create database ====
 
==== Create database ====
 
To create the first database, which we will call "mydb", simply type :
 
To create the first database, which we will call "mydb", simply type :
 
<pre><nowiki>
 
<pre><nowiki>
sudo -u postgres createdb mydb
+
sudo -u postgres createdb mydb
 
</nowiki></pre>
 
</nowiki></pre>
 +
=== Alternative Server Setup ===
 +
If you don't intend to connect to the database from other machines, this alternative setup may be simpler.
 +
By default in Ubuntu, Postgresql is configured to use 'ident sameuser' authentication for any connections from the same machine.  Check out the excellent Postgresql documentation for more information, but essentially this means that if your Ubuntu username is 'foo' and you add 'foo' as a Postgresql user then you can connect to the database without requiring a password.
 +
Since the only user who can connect to a fresh install is the postgres user, here is how to create yourself a database account (which is in this case also a database superuser) with the same name as your login name and then create a password for the user:
 +
<pre><nowiki>
 +
sudo -u postgres createuser --superuser $USER
 +
sudo -u postgres psql
 +
</nowiki></pre>
 +
<pre><nowiki>
 +
postgres=# \password $USER
 +
 +
</nowiki></pre>
 +
Client programs, by default, connect to the local host using your Ubuntu login name and expect to find a database with that name too.  So to make things REALLY easy, use your new superuser privileges granted above to create a database with the same name as your login name:
 +
<pre><nowiki>
 +
createdb $USER
 +
</nowiki></pre>
 +
Connecting to your own database to try out some SQL should now be as easy as:
 +
<pre><nowiki>
 +
psql
 +
</nowiki></pre>
 +
Creating additional database is just as easy, so for example, after running this:
 +
<pre><nowiki>
 +
createdb amarokdb
 +
</nowiki></pre>
 +
You can go right ahead and tell Amarok to use postgresql to store its music catalog. The database name would be amarokdb, the username would be your own login name, and you don't even need a password thanks to 'ident sameuser' so you can leave that blank.
 
=== Using pgAdmin III  GUI ===
 
=== Using pgAdmin III  GUI ===
To get an idea of what [[UbuntuHelp:PostgreSQL|PostgreSQL]] can do, you may start by firing up a graphical client. In a terminal type :
+
To get an idea of what PostgreSQL can do, you may start by firing up a graphical client. In a terminal type :
 
<pre><nowiki>
 
<pre><nowiki>
pgadmin3
+
pgadmin3
 
</nowiki></pre>
 
</nowiki></pre>
 
You will be presented with the pgAdmin III interface. Click on the "Add a connection to a server" button (top left). In the new dialog, enter the address 127.0.0.1, a description of the server, the default database ("mydb" in the example above), your username ("postgres") and your password.
 
You will be presented with the pgAdmin III interface. Click on the "Add a connection to a server" button (top left). In the new dialog, enter the address 127.0.0.1, a description of the server, the default database ("mydb" in the example above), your username ("postgres") and your password.
第47行: 第87行:
 
=== Managing the Server ===
 
=== Managing the Server ===
 
==== Managing users and rights ====
 
==== Managing users and rights ====
[[UbuntuHelp:PostgreSQL|PostgreSQL]] has a not really easy way to manage users. To manage users, you first have to edit <code><nowiki>/etc/postgresql/8.1/main/pg_hba.conf</nowiki></code> and modify the default configuration which is very protective. For example, if you want <code><nowiki>postgres</nowiki></code> to manage its own users (not linked with system users), you will add the following line:
+
To manage users, you first have to edit <code><nowiki>/etc/postgresql/8.1/main/pg_hba.conf</nowiki></code> and modify the default configuration which is very protective. For example, if you want <code><nowiki>postgres</nowiki></code> to manage its own users (not linked with system users), you will add the following line:
 
<pre><nowiki>
 
<pre><nowiki>
 
8<-------------------------------------------
 
8<-------------------------------------------
 
# TYPE  DATABASE    USER        IP-ADDRESS        IP-MASK          METHOD
 
# TYPE  DATABASE    USER        IP-ADDRESS        IP-MASK          METHOD
host    all        all        10.0.0.0      255.255.255.0    password
+
host    all        all        10.0.0.0      255.255.255.0    md5
 
8<-------------------------------------------
 
8<-------------------------------------------
 
</nowiki></pre>
 
</nowiki></pre>
 
Which means that on your local network (10.0.0.0/24 - replace with your own local network !),  postgres users can connect through the network to the database providing a classical couple user / password.
 
Which means that on your local network (10.0.0.0/24 - replace with your own local network !),  postgres users can connect through the network to the database providing a classical couple user / password.
 +
Besides allowing a user to connect over the network to the to a database on the server, you must enable postgre to listen across different networks. To do that, open up <code><nowiki>/etc/postgresql/8.3/main/postgresql.conf</nowiki></code> in your favourite editor and alter the <code><nowiki>listen_addresses</nowiki></code> as below:
 +
<pre><nowiki>
 +
listen_addresses = '10.0.0.1,localhost'
 +
</nowiki></pre>
 +
where <code><nowiki>10.0.0.1</nowiki></code> describes your IP address on the local network. Note that this setting is commented out by default.
 
To create a database with a user that have full rights on the database, use the following command:
 
To create a database with a user that have full rights on the database, use the following command:
 
<pre><nowiki>
 
<pre><nowiki>
第63行: 第108行:
 
This little example will probably suit most of your needs. For more details, please refer to the corresponding man pages or the online documentation.
 
This little example will probably suit most of your needs. For more details, please refer to the corresponding man pages or the online documentation.
 
=== Further reading ===
 
=== Further reading ===
If you are not familiar with [http://en.wikipedia.org/wiki/Sql SQL] you may want to look into this powerful language, although some simple uses of [[UbuntuHelp:PostgreSQL|PostgreSQL]] may not require this knowledge (such as a simple [http://www.djangoproject.com Django] project).
+
If you are not familiar with [http://en.wikipedia.org/wiki/Sql SQL] you may want to look into this powerful language, although some simple uses of PostgreSQL may not require this knowledge (such as a simple [http://www.djangoproject.com Django] project).
The [http://www.postgresql.org/ [[UbuntuHelp:PostgreSQL|PostgreSQL]] website] contains a wealth of information on using this database.
+
The [http://www.postgresql.org/ PostgreSQL website] contains a wealth of information on using this database.
 
----
 
----
 
<sup>[1]</sup> You do not have to pay in order to use PostgreSQL for some applications, such as commercial closed source software, contrary to other well known open source databases.
 
<sup>[1]</sup> You do not have to pay in order to use PostgreSQL for some applications, such as commercial closed source software, contrary to other well known open source databases.
<br>
+
<<BR>>
 
'''Note:''' this guide has been tested on Ubuntu 6.06 (Dapper) and Ubuntu 7.10 (Gutsy)
 
'''Note:''' this guide has been tested on Ubuntu 6.06 (Dapper) and Ubuntu 7.10 (Gutsy)
 +
=== External Links ===
 +
The following are unverified suggestions made by users.
 +
==== Turnkey Linux ====
 +
An Ubuntu-based [[UbuntuHelp: http://www.turnkeylinux.org/appliances/postgresql | PostgreSQL appliance ]] is one of the easiest ways to get up and running with  PostgreSQL on Ubuntu. It's part of a family of pre-integrated [[UbuntuHelp: http://www.turnkeylinux.org/ | TurnKey Linux ]] [[UbuntuHelp: http://www.turnkeylinux.org/appliances | Software Appliances ]] based on Ubuntu 8.04.2 (Hardy LTS). 
 
----
 
----
[[category:CategoryDocumentation]]
 
  
 
[[category:UbuntuHelp]]
 
[[category:UbuntuHelp]]

2010年5月19日 (三) 23:56的最新版本

Introduction

PostgreSQL is a powerful object-relational database management system, provided under a flexible BSD-style license.[1] PostgreSQL contains many advanced features, is very fast and standards compliant. PostgreSQL has bindings for many programming languages such as C, C++, Python, Java, PHP, Ruby... It can be used to power anything from simple web applications to massive databases with millions of records.

Installation

Dapper

To install PostgreSQL 8.1 you may use the command line and type:

 sudo apt-get install postgresql-8.1

Hardy

Install the latest Postgresql 8.3 from the command line with the following command:

 sudo apt-get install postgresql

Karmic

To install Postgresql 8.4 you may use the command line and type:

 sudo apt-get install postgresql

Lucid

To install Postgresql 8.4 you may use the command line and type:

 sudo apt-get install postgresql

Administration

pgAdmin III is a handy GUI for PostgreSQL, it is essential to beginners. To install it, type at the command line:

 sudo apt-get install pgadmin3

You may also use the Synaptic package manager from the System>Administration menu to install these packages.

Basic Server Setup

To start off, we need to change the PostgreSQL postgres user password; we will not be able to access the server otherwise. As the “postgres” Linux user, we will execute the psql command. In a terminal, type:

Dapper

sudo -u postgres psql template1

Karmic and Lucid

sudo -u postgres psql postgres

Set a password for the "postgres" database role using the command:

\password postgres

and give your password when prompted. The password text will be hidden from the console for security purposes. Type Control+D to exit the posgreSQL prompt.

Create database

To create the first database, which we will call "mydb", simply type :

 sudo -u postgres createdb mydb

Alternative Server Setup

If you don't intend to connect to the database from other machines, this alternative setup may be simpler. By default in Ubuntu, Postgresql is configured to use 'ident sameuser' authentication for any connections from the same machine. Check out the excellent Postgresql documentation for more information, but essentially this means that if your Ubuntu username is 'foo' and you add 'foo' as a Postgresql user then you can connect to the database without requiring a password. Since the only user who can connect to a fresh install is the postgres user, here is how to create yourself a database account (which is in this case also a database superuser) with the same name as your login name and then create a password for the user:

 sudo -u postgres createuser --superuser $USER
 sudo -u postgres psql
 postgres=# \password $USER
 

Client programs, by default, connect to the local host using your Ubuntu login name and expect to find a database with that name too. So to make things REALLY easy, use your new superuser privileges granted above to create a database with the same name as your login name:

 createdb $USER

Connecting to your own database to try out some SQL should now be as easy as:

 psql

Creating additional database is just as easy, so for example, after running this:

 createdb amarokdb

You can go right ahead and tell Amarok to use postgresql to store its music catalog. The database name would be amarokdb, the username would be your own login name, and you don't even need a password thanks to 'ident sameuser' so you can leave that blank.

Using pgAdmin III GUI

To get an idea of what PostgreSQL can do, you may start by firing up a graphical client. In a terminal type :

 pgadmin3

You will be presented with the pgAdmin III interface. Click on the "Add a connection to a server" button (top left). In the new dialog, enter the address 127.0.0.1, a description of the server, the default database ("mydb" in the example above), your username ("postgres") and your password. With this GUI you may start creating and managing databases, query the database, execute SQl etc.

Managing the Server

Managing users and rights

To manage users, you first have to edit /etc/postgresql/8.1/main/pg_hba.conf and modify the default configuration which is very protective. For example, if you want postgres to manage its own users (not linked with system users), you will add the following line:

8<-------------------------------------------
# TYPE  DATABASE    USER        IP-ADDRESS        IP-MASK           METHOD
host    all         all         10.0.0.0       255.255.255.0    md5
8<-------------------------------------------

Which means that on your local network (10.0.0.0/24 - replace with your own local network !), postgres users can connect through the network to the database providing a classical couple user / password. Besides allowing a user to connect over the network to the to a database on the server, you must enable postgre to listen across different networks. To do that, open up /etc/postgresql/8.3/main/postgresql.conf in your favourite editor and alter the listen_addresses as below:

listen_addresses = '10.0.0.1,localhost'

where 10.0.0.1 describes your IP address on the local network. Note that this setting is commented out by default. To create a database with a user that have full rights on the database, use the following command:

sudo -u postgres createuser -D -A -P myuser
sudo -u postgres createdb -O myuser mydb

The first command line creates the user the no database creation rights (-D) with no add user rights -A) and will prompt you for entering a password (-P). The second command line create the database 'mydb with 'myuser' as owner. This little example will probably suit most of your needs. For more details, please refer to the corresponding man pages or the online documentation.

Further reading

If you are not familiar with SQL you may want to look into this powerful language, although some simple uses of PostgreSQL may not require this knowledge (such as a simple Django project). The PostgreSQL website contains a wealth of information on using this database.


[1] You do not have to pay in order to use PostgreSQL for some applications, such as commercial closed source software, contrary to other well known open source databases. <
> Note: this guide has been tested on Ubuntu 6.06 (Dapper) and Ubuntu 7.10 (Gutsy)

External Links

The following are unverified suggestions made by users.

Turnkey Linux

An Ubuntu-based PostgreSQL appliance is one of the easiest ways to get up and running with PostgreSQL on Ubuntu. It's part of a family of pre-integrated TurnKey Linux Software Appliances based on Ubuntu 8.04.2 (Hardy LTS).