First, install
With Ubuntu, for example, if you need to install the latest version, just use:
sudo apt-get install postgresql
If you want to install the latest version to Ubuntu Trusty (14.04), for example.
- Start PostgreSQL download page to obtain the corresponding Apt warehouse information, and then create a file /etc/apt/sources.list.d/pgdg.list , the command is:
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
- Load warehouse GPG Key:
wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
- installation
sudo apt-get update && sudo apt-get install postgresql postgresql-contrib
Second, open a command line to create the database
After installing PostgreSQL, automatically creates a database system named postgres user name and role name, and create a user with the same username in Linux.
Switch to postgres user:
sudo su - postgres
Open psql terminal:
psql
Since the database system postgres user password is randomly generated, if you want to modify, by psql
performing the open terminal window:
ALTER USER postgres WITH PASSWORD 'postgres';
If you want to change the user's password postgres Linux system:
- Delete the old password:
sudo passwd -d postgres
- Set a new password:
sudo su - postgres
passwd
Create a database
The story is set: Batman want to create a database of registered asshole:
CREATE DATABASE villains;
You can also specify the encoding when creating a database:
CREATE DATABASE villains ENCODING 'UTF-8';
Third, create user
To ensure safety, create a dedicated database for each user:
CREATE USER batman WITH PASSWORD 'Extremly-Secret-Password';
Fourth, the user delegated authority
All authorized permissions:
GRANT ALL PRIVILEGES ON DATABASE villains to batman;
Or you can authorize specified permission:
GRANT SELECT ON DATABASE villains to alfred;
Able to authorize permissions are:
- SELECT
- INSERT
- UPDATE
- DELETE
- RULE
- REFERENCES
- TRIGGER
- CREATE
- TEMPORARY
- EXECUTE
- USAGE
For the above to create the database, create a user and authorize a three-step operation, in fact, can be done in one step:
CREATE DATABASE villains OWNER batman;
Fifth, create a table
Create a super asshole data sheet:
CREATE TABLE super_villains (id serial PRIMARY KEY, name character varying(100), super_power character varying(100), weakness character varying(100));
Then create a data table equipped with:
CREATE TABLE equipment (id serial PRIMARY KEY, name character varying(100), status character varying(100), special_move character varying(100));
Sixth, the display information table
View table column names, column table describes the types of information:
\d+ equipment;
If you would like to check on PgAdmin words:
SELECT *
FROM information_schema.columns
WHERE table_name = 'equipment';
This command displays more detailed information on the table.
If you want to list the current database in which tables:
\d
This command will list all of the tables in the database, including some control table created by the system, such as equipment_id_seq and so on.
Seven, modify the table, so that number can be incremented
System has created the control table corresponding to the table number increment, such as the correspondence table has equipment_id_seq equipment table.
Modify the table so that each need to fill id value inserted, id auto-increment value can be:
ALTER TABLE equipment ALTER id set default nextval('equipment_id_seq');
ALTER TABLE super_villains ALTER id set default nextval('super_villains_id_seq');
Eight, insert data
INSERT INTO equipment(name, status, special_move) VALUES('Utility Belt', 'Nice and yellow', 'All kind of cool stuff in your waist');
INSERT INTO super_villains(name, super_power, weakness) VALUES('The Joker', 'Extra Crazy', 'Super punch');
IX inquiry
SELECT * from equipment;
SELECT * from super_villains;
SELECT weakness from super_villains;
SELECT special_move, name from equipment;
Ten, basic management operations
Allow remote access
Find the configuration file database postgresql.conf , position on the 14.04 Ubuntu is /etc/postgresql/9.5/main/postgresql.conf .
Found containing #listen_address = 'localhost'
rows modified to allow access to the remote IP address, such as: listen_addresses = '10.0.0.3'
, or allow all addresses: listen_addresses = '*'
.
Restart the database server
On Ubuntu:
sudo service postgresql restart
XI, backup and recovery
Backup
$ pg_dump --username=name dbname > backup.sql
reduction
$ sudo su - postgres
$ dropdb dbname # 先删除原来的数据库
$ createdb --owner=owner dbname
$ psql -d dbname -f backup.sql
references: