student$ /usr/local/pgsql-9.4/bin/psql
=> CREATE USER dbuser PASSWORD 'mypassword';
CREATE ROLE
Исправим pg_hba.conf (добавим нужную строку в начало файла):
postgres$ cd /usr/local/pgsql-9.4/data; { echo "local all dbuser md5"; cat pg_hba.conf; } > pg_hba.conf.new; mv pg_hba.conf{.new,}
Вот что получилось (не считая комментариев):
postgres$ egrep "^[^#]" /usr/local/pgsql-9.4/data/pg_hba.conf
local all dbuser md5 local all all trust host all all 127.0.0.1/32 trust host all all ::1/128 trust
Добавим пароль в .pgpass, чтобы не вводить его вручную (при выполнении задания этот шаг лучше пропустить: будет непонятно, срабатывает аутентификация по паролю или безусловный доступ, включенный по умолчанию).
student$ echo '*:*:*:dbuser:mypassword' > /home/student/.pgpass
student$ chmod 0600 /home/student/.pgpass
Сначала создадим базу данных.
=> CREATE DATABASE admin_upgrade;
CREATE DATABASE
=> \c admin_upgrade
You are now connected to database "admin_upgrade" as user "postgres".
Тип данных hstore доступен в расширении hstore.
student$ sudo make -C /home/student/postgresql-9.4.8/contrib/hstore install | tail
make: Entering directory '/home/student/postgresql-9.4.8/contrib/hstore' /bin/mkdir -p '/usr/local/pgsql-9.4/lib' /bin/mkdir -p '/usr/local/pgsql-9.4/share/extension' /bin/mkdir -p '/usr/local/pgsql-9.4/share/extension' /usr/bin/install -c -m 755 hstore.so '/usr/local/pgsql-9.4/lib/hstore.so' /usr/bin/install -c -m 644 hstore.control '/usr/local/pgsql-9.4/share/extension/' /usr/bin/install -c -m 644 hstore--1.3.sql hstore--1.2--1.3.sql hstore--1.1--1.2.sql hstore--1.0--1.1.sql hstore--unpackaged--1.0.sql '/usr/local/pgsql-9.4/share/extension/' make: Leaving directory '/home/student/postgresql-9.4.8/contrib/hstore'
=> CREATE EXTENSION hstore;
CREATE EXTENSION
Переключимся под пользователя dbuser и создадим таблицу.
=> \c - dbuser
You are now connected to database "admin_upgrade" as user "dbuser".
=> CREATE TABLE test(id serial, kv hstore);
CREATE TABLE
=> INSERT INTO test(kv) VALUES ('a=>1,b=>2'::hstore);
INSERT 0 1
=> INSERT INTO test(kv) VALUES ('c=>3,d=>4'::hstore);
INSERT 0 1
student$ /usr/local/pgsql-9.4/bin/pg_dumpall > /home/student/dump.sql
Добавим табличное пространство по умолчанию:
student$ grep 'CREATE DATABASE admin_upgrade' /home/student/dump.sql
CREATE DATABASE admin_upgrade WITH TEMPLATE = template0 OWNER = postgres;
student$ sed -i 's/\(CREATE DATABASE admin_upgrade WITH\)/\1 TABLESPACE = ts/' /home/student/dump.sql
student$ grep 'CREATE DATABASE admin_upgrade' /home/student/dump.sql
CREATE DATABASE admin_upgrade WITH TABLESPACE = ts TEMPLATE = template0 OWNER = postgres;
Останавливаем сервер.
=> \q
postgres$ /usr/local/pgsql-9.4/bin/pg_ctl -w -D /usr/local/pgsql-9.4/data stop
waiting for server to shut down.... done server stopped
Изменяем pg_hba.conf:
postgres$ cd /usr/local/pgsql-10/data; { echo "local all dbuser md5"; cat pg_hba.conf; } > pg_hba.conf.new; mv pg_hba.conf{.new,}
Вот что получилось:
postgres$ egrep "^[^#]" /usr/local/pgsql-10/data/pg_hba.conf
local all dbuser md5 local all all trust host all all 127.0.0.1/32 trust host all all ::1/128 trust local replication all trust host replication all 127.0.0.1/32 trust host replication all ::1/128 trust
Видно, что в версии 10 содержимое pg_hba.conf изменилось; потому не стоит просто копировать старый файл.
Настройки postgresql.conf не переносим, хотя в реальной жизни это, конечно, необходимо.
Создаем каталог для табличного пространства.
postgres$ rm -rf /home/postgres/ts_dir
postgres$ mkdir /home/postgres/ts_dir
Стартуем кластер 10 и создаем табличное пространство.
postgres$ /usr/local/pgsql-10/bin/pg_ctl -w -l /home/postgres/logfile-10 -D /usr/local/pgsql-10/data start
waiting for server to start.... done server started
student$ /usr/local/pgsql-10/bin/psql
=> CREATE TABLESPACE ts LOCATION '/home/postgres/ts_dir';
CREATE TABLESPACE
=> \q
Создаем расширение:
student$ sudo make -C /home/student/postgresql-10.6/contrib/hstore install | tail
make: Entering directory '/home/student/postgresql-10.6/contrib/hstore' /bin/mkdir -p '/usr/local/pgsql-10/lib' /bin/mkdir -p '/usr/local/pgsql-10/share/extension' /bin/mkdir -p '/usr/local/pgsql-10/share/extension' /usr/bin/install -c -m 755 hstore.so '/usr/local/pgsql-10/lib/hstore.so' /usr/bin/install -c -m 644 ./hstore.control '/usr/local/pgsql-10/share/extension/' /usr/bin/install -c -m 644 ./hstore--1.4.sql ./hstore--1.3--1.4.sql ./hstore--1.2--1.3.sql ./hstore--1.1--1.2.sql ./hstore--1.0--1.1.sql ./hstore--unpackaged--1.0.sql '/usr/local/pgsql-10/share/extension/' make: Leaving directory '/home/student/postgresql-10.6/contrib/hstore'
Восстанавливаем кластер из резервной копии:
student$ /usr/local/pgsql-10/bin/psql -f /home/student/dump.sql
SET SET SET CREATE ROLE ALTER ROLE psql:/home/student/dump.sql:16: ERROR: role "postgres" already exists ALTER ROLE CREATE DATABASE REVOKE REVOKE GRANT GRANT You are now connected to database "admin_upgrade" as user "postgres". SET SET SET SET SET SET SET CREATE EXTENSION COMMENT CREATE EXTENSION COMMENT SET SET SET CREATE TABLE ALTER TABLE CREATE SEQUENCE ALTER TABLE ALTER SEQUENCE ALTER TABLE COPY 2 setval -------- 2 (1 row) REVOKE REVOKE GRANT GRANT You are now connected to database "postgres" as user "postgres". SET SET SET SET SET SET SET COMMENT CREATE EXTENSION COMMENT REVOKE REVOKE GRANT GRANT You are now connected to database "template1" as user "postgres". SET SET SET SET SET SET SET COMMENT CREATE EXTENSION COMMENT REVOKE REVOKE GRANT GRANT
При создании роли postgres выдается ошибка, поскольку такая роль уже существует; это нормально.
student$ /usr/local/pgsql-10/bin/psql -d admin_upgrade -U dbuser
=> \conninfo
You are connected to database "admin_upgrade" as user "dbuser" via socket in "/tmp" at port "5433".
=> SELECT version();
version --------------------------------------------------------------------------------------------------------------- PostgreSQL 10.6 on i686-pc-linux-gnu, compiled by gcc (Ubuntu 5.4.0-6ubuntu1~16.04.10) 5.4.0 20160609, 32-bit (1 row)
=> SELECT * from test;
id | kv ----+-------------------- 1 | "a"=>"1", "b"=>"2" 2 | "c"=>"3", "d"=>"4" (2 rows)
=> SELECT pg_relation_filepath('test');
pg_relation_filepath --------------------------------------------- pg_tblspc/16384/PG_10_201707211/16386/16510 (1 row)