Showing posts with label PostgreSQL. Show all posts
Showing posts with label PostgreSQL. Show all posts

Upgrade PostgreSQL from 9.1 to 9.3 on Ubuntu

At the time of writing, you could not install postgreSQL 9.3 on Ubuntu using apt-get. Here is simple instruction of updating postgreSQL on Ubuntu to v9.3
#install dependency
sudo apt-get update
sudo apt-get -y install python-software-properties
#add repository
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
#setuop repository
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main" >> /etc/apt/sources.list.d/postgresql.list'
#install
sudo apt-get install postgresql-9.3 postgresql-server-dev-9.3 postgresql-contrib-9.3
#upgrade
sudo su -l postgres
psql -d template1 -p 5433
CREATE EXTENSION IF NOT EXISTS hstore;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
\q #logout from database
service postgresql stop
/usr/lib/postgresql/9.3/bin/pg_upgrade -b /usr/lib/postgresql/9.1/bin -B /usr/lib/postgresql/9.3/bin -d /var/lib/postgresql/9.1/main/ -D /var/lib/postgresql/9.3/main/ -O " -c config_file=/etc/postgresql/9.3/main/postgresql.conf" -o " -c config_file=/etc/postgresql/9.1/main/postgresql.conf"
exit # logout postgresql back to previous user
sudo apt-get remove postgresql-9.1
sudo vim /etc/postgresql/9.3/main/postgresql.conf # find old port of 5433 and change it to 5432
sudo service postgresql restart
Issues you may encounter: when you run the real update command above (/usr/lib/postgresql/9.3/bin/pg_upgrade...), you may notice failure and show error of "Problem of connection to database failed: fe_sendauth: no password supplied " The solution would be modify pg_hba.conf of two data directories in trust mode (NO PASSWORD) authentication then re-run pg_upgrade command. Example, # Database administrative login by Unix domain socket local all postgres trust

postgreSQL: Multiple schemas in one database

How to query a table from other schema than "pulic" schema?

If a database contains more than one named schemas, you can query use schema_name.table_name to query the content of the table.

select * from myschema.mytable;

create table myschema.mytable(

......

);

You alswaysa can simplify this by changing the search path.

SHOW search_path;

will show the current search path. The follwoing command will add the schema myachema into the search_path. so next time you do not need to use myschema.mytable to query data.

SET search_path TO myschema,public;

Chado and postgreSQL: ERROR: function create_point(integer, integer) does not exist

I have two chado databases installed locally. One is individually installed the other one is installed insideTripal.

While I am running queries with the Chado database which is installed inside Tripal, it gives me some error message sometimes,

ERROR:  function create_point(integer, integer) does not exist

To solve this problem I have to create the functions by myself. it seems that the funsctin were not preloaded into the Chado schema insides tripal.

Query I issued on this database:

SELECT feature.uniquename FROM chado.feature LEFT JOIN chado.featureloc fl ON (chado.feature.feature_id = fl.feature_id)LEFT JOIN chado.feature srcf ON (fl.srcfeature_id = srcf.feature_id) WHERE srcf.name = 'IncAC_plasmid_SH163';

 

links: http://generic-model-organism-system-database.450254.n5.nabble.com/Error-at-the-time-of-chado-database-import-on-frange-featuregoup-foreign-keys-td460184.html

Backup and Restore Postgresql Database

Part one:

create database:

$ su postgres

bash-3.2$ createdb db_name;

bash-3.2$ dropdb db_name;

Or you can go into database:

$ psql -d db_name;

db_name=# create database new_db_name;

db_name=# drop database new_db_name;



To backup a Postgresql Database:

$ pg_dump -U db_user_name database_name -f backup_file_name.sql

This command is to backup the database_name to file backup_file_name.sql.



To backup a specific schema of the database, use -n parameter

$ pg_dump -U htang tripal_dev -n chado -f ~/DBBackups/tripal_dev__chado_only_20110901.sql

To restore a backup file use the follwing command:

$ psql -f backup_file_path_and_name DATABASE_name;

  or you can

$ psql -d database_name < backup_file_path_and_name;

http://developer.postgresql.org/pgdocs/postgres/app-pgdump.html

Set PostGreSQL to accept remote access

Step # 1: Enable client authentication

Edit the PostgreSQL configuration file /var/lib/pgsql/data/pg_hba.conf:

# TYPE DATABASE USER CIDR-ADDRESS METHOD

# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 10.14.0.0/0 password
# IPv6 local connections:
host all all ::1/128 password

Save and close the file.

Step # 2: Enable networking for PostgreSQL: Allow TCP/IP socket

If you are using PostgreSQL version 8.x or newer use the following instructions or skip to Step # 3a for older version (7.x or older).

You need to open PostgreSQL configuration file /var/lib/pgsql/data/postgresql.conf

In this file change the following:

listen_addresses='localhost'

to

listen_addresses='*'

Step # 3 Restart PostgreSQL Server

Type the following command:
# /etc/init.d/postgresql restart

Step # 4: Iptables firewall rules

Make sure iptables is not blocking communication, open port 5432 (append rules to your iptables scripts or file /etc/sysconfig/iptables):

put this line in the iptables,

-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT

But it must before this line,

-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited

Restart firewall:
# /etc/init.d/iptables restart
Step # 6: Test your setup

Modified from http://www.cyberciti.biz/tips/postgres-allow-remote-access-tcp-connection.html

**Try to disable the Iptable if you still can not work it out.

Install PostgreSQL V9.x on Centos

1. Configure your YUM repository Since there is no v9.x in yum repository at the time of writing, you could not use yum to intall postgres without reconfiguration of yum repository. In order to do so: Edit /etc/yum.repos.d/CentOS-Base.repo, add the following lines to both [base] and [updates] sections exclude=postgresql* Then you need download and install PGDG RPM file for for your version of the CentOS from here. Here is the link for 64 Bit version of CentOS.
wget http://yum.postgresql.org/9.2/redhat/rhel-5-x86_64/pgdg-centos92-9.2-6.noarch.rpm
rpm -ivh pgdg-centos92-9.2-6.noarch.rpm
2. Install postgreSQL After these modifications you could use yum to install postgreSQl versino 9.2. I installed both postgresql-server and postgresql-devel
yum install postgresql-server postgresql-devel
3. Post-installation configuration 3.1. Initialize The first command (only needed once) is to initialize the database in PGDATA:
/sbin/service postgres-9.2 initdb
3.2. Startup
/etc/init.d/postgresql-9.2 start
If you want PostgreSQL to start automatically when CentOS starts:
/sbin/chkconfig postgresql-9.2 on
3.3.Set PostgreSQL 9 Environment The deault home directory for the user postgres is at /var/lib/pgsql The bash_profile for the user postgres will look like this:
[ -f /etc/profile ] && source /etc/profile
PGDATA=/var/lib/pgsql/9.2/data
export PGDATA
This contains a path for the data directory, but no path for the executable/binary directory. To ammend this, add the path as below:
[ -f /etc/profile ] && source /etc/profile
PGDATA=/var/lib/pgsql/9.2/data
export PGDATA
PATH=$PATH:$HOME/bin:/usr/pgsql-9.2/bin
export PATH
Placing the binary directory in the path for postgres will allow you to invoke pg_ctl and other commands from the shell. 3.4. Add User The superuser postgres has no password set by default. To set the password, switch to postgres user:
# Linux environment
sudo su -
passwd postgres
# follow the screen to supply the new password for postgres
Connect to postgres database as user postgres.
psql postgres postgres
postgres=#
postgres=# create user myuser with password 'secret';
if you forget to assign a password to new use, you could do it by going to the database environment:
#postgresql environment
alter user user_name with password='new_password'
3.5. Create a database and give ownership to the new user:
postgres=# create database mytestdb owner=myuser;
Connect to the database as new user:
postgres=# \c mytestdb myuser
Password for user myuser:
You are now connected to database "mytestdb" as user "myuser".
3.6. Other configurations Modifications to the configure file to allow postgresql accept local/remote access Step # 1: Enable client authentication Edit the PostgreSQL configuration file /var/lib/pgsql/9.2/data/pg_hba.conf:
# TYPE DATABASE USER CIDR-ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 trust
host all all 10.14.0.0/0 password
# IPv6 local connections:
host all all ::1/128 password
# Save and close the file. In order for the change to take effect, reload the pg_hba.conf file.
su - postgres
pg_ctl reload
Step # 2: Enable networking for PostgreSQL: Allow TCP/IP socket If you are using PostgreSQL version 8.x or newer use the following instructions: Open PostgreSQL configuration file /var/lib/pgsql/9.2/data/postgresql.conf and change
listen_addresses='localhost'
to
listen_addresses='*'
Step # 3 Restart PostgreSQL Server
/etc/init.d/postgresql-9.2 restart
Step # 4: Modify Iptables firewall rules Make sure iptables is not blocking communication, open port 5432 (append rules to your iptables scripts or file /etc/sysconfig/iptables): put this line in the iptables,
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 5432 -j ACCEPT
But it must before this line,
-A RH-Firewall-1-INPUT -j REJECT --reject-with icmp-host-prohibited
Restart firewall:
/etc/init.d/iptables restart
All set.

Import Excel File into PostgreSQL database

1. Create table in the database which you want to use to hold the content in the excel file using create table command. For simple you can create a table with exactly same field name as in the excel file.
2. delete the field names from the excel file if there is any.
3. Save the excel into CVS (Comma separated file) file.
4. use the copy command to transfer data from CVS file into database.
copy table_name from 'source_file_with_path' DELIMITERS ',' CSV;
for example,
copy student_19 from 'E://ftproot//Student_19.csv' DELIMITERS ',' CSV;

Set the start value of the SERIAL type data in PostgreSQL database

I have been searing online to find a way to set the default start value of the serial type data use in the alter command without success.
But I did have an alternative solution to solve this problem by first defining the sequence, which is used by the serial type:
create sequence seqnbr_student_17 start 588280;
alter table student_17 add seqnbr integer not null default nextval(‘seqnbr_student_17 ’);
By doing this two lines of code you have actually defined a serial field with the start value 588280.

How to transfer postgresSQL data from Windows XP (higher version) to Ubuntu (lower version)

In this case the pgAdmin in my Xp is 1.11 and in Ubuntu is 1.8.2, that means I can not do it through backup and restore. if you use it, you may get some error like versino does not match: [archive]unsupported version (1.11) in file header.
So I have to use pg_dump command:
In WIndows XP:
in the command window type in:


c:\Program Files\PostgreSQL\8.4\bin\pg_dump -U table_name DATABASE_name > backup_file_path_and_name;

Type in user password when needed
In Ubuntu:
Locate the psql command, usually it will be in the following directory: in /usr/bin:
Type in the following command in terminal:

cd /usr/bin;
psql -f backup_file_path_and_name DATABASE_name;


waiting for the process to finish.

PostgreSQL Database Cluster Initialisation Failed Solution

How to install postgreSQL on Windows Server 2008 64Bit

I tried several times to install postgreSQl on Windows Server 2008, but failed due to "PostgreSQL Database Cluster Initialization".
I tried to install it to another path instead of c:\Program files(x86), but no success. Then I follow one solution online proposed for vista, adding full control power for the use postgres on the installing folder. Again there is no success.
Then I tried to install it through the binariew installer. The one I used is v8.3.7.

Error : “ 'more' is not recognized as an internal or external command, operable program or batch file. "

When you type in \dt and the postgreSQl pops " 'more' is not recognized as an internal or external command, operable program or batch file. "

This is because "more" exists in C:\Windows\System32 and you did not add it to your PATH Environment Variable.
To do so, My Computer (Right Click) --> Properties --> then go to Advanced tab --> Environment Variable --> set the path by adding C:\Windows\System32. Be sure to use ; to separate from other paths may exit here.

$2222

PostgreSQL常规用法

PostgreSQL的实用程序:
(1)用户实用程序:
createdb 创建一个新的PostgreSQL的数据库(和SQL语句:CREATE DATABASE 相同)
createuser 创建一个新的PostgreSQL的用户(和SQL语句:CREATE USER 相同)
dropdb 删除数据库
dropuser 删除用户
pg_dump 将PostgreSQL数据库导出到一个脚本文件
pg_dumpall 将所有的PostgreSQL数据库导出到一个脚本文件
pg_restore 从一个由pg_dump或pg_dumpall程序导出的脚本文件中恢复PostgreSQL数据库
psql 一个基于命令行的PostgreSQL交互式客户端程序
vacuumdb 清理和分析一个PostgreSQL数据库,它是客户端程序psql环境下SQL语句VACUUM的shell脚本封装,二者功能完全相同
(2)系统实用程序
initdb 创建一个用于存储数据库的PostgreSQL数据目录,并创建预定义的模板数据库template0和template1,生成共享目录表catalog;此程序通常只在安装PostgreSQL时运行一次
initlocation 创建一个辅助的PostgreSQL数据库存储区域
ipcclean 从停止的PostgreSQL服务器中清除共享内在和孤立信号标志
pg_ctl 启动、停止、重启PostgreSQL服务(比如:pg_ctl start 启动PostgreSQL服务,它和service postgresql start相同)
pg_controldata 显示PostgreSQL服务的内部控制信息
postgres PostgreSQL单用户模式的数据库服务
postmaster PostgreSQL多用户模式的数据库服务
4.这里面最重要的是psql这个客户端程序最为重要。启用客户端程序psql的方法是:
切换到PostgreSQL预定义的数据库超级用户postgres,启用客户端程序psql,并连接到自己想要的数据库,比如说:
psql template1
出现以下界面,说明已经进入到想要的数据库,可以进行想要的操作了。
template1=#
5.在数据库中的一些命令:
template1=# \l 查看系统中现存的数据库
template1=# \q 退出客户端程序psql
template1=# \c 从一个数据库中转到另一个数据库中,如template1=# \c sales 从template1转到sales
template1=# \dt 查看表
template1=# \d 查看表结构
template1=# \di 查看索引
6.要注意随时对数据库进行清理、收回磁盘空间并更新统计信息,使用下面的命令就搞定!
vaccumdb -d sales -z
-a 对所有的数据库操作
-z 保证不断地删除失效的行,节约磁盘空间,将统计信息更新为最近的状态
7.PostgreSQL用户认证
PostgreSQL数据目录中的pg_hba.conf的作用就是用户认证,可以在/var/lib/pgsql/data中找到。
有以下几个例子可以看看:
(1)允许在本机上的任何身份连接任何数据库
TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
local all all trust(无条件进行连接)
(2)允许IP地址为192.168.1.x的任何主机与数据库sales连接
TYPE DATABASE USER IP-ADDRESS IP-MASK METHOD
host sales all 192.168.1.0 255.255.255.0 ident sameuser(表明任何操作系统用户都能够以同名数据库用户进行连接)
8.看了那么多,来一个完整的创建PostgreSQL数据库用户的示例吧
(1)进入PostgreSQL高级用户
(2)启用客户端程序,并进入template1数据库
psql template1
(3)创建用户
template1=# CREATE USER hellen WITH ENCRYPED PASSWORD'zhenzhen'
(4)因为设置了密码,所以要编辑pg_hba.conf,使用户和配置文件同步。
在原有记录上面添加md5
local all hellen md5
(4)使用新用户登录数据库
template1=# \q
psql -U hellen -d template1
PS:在一个数据库中如果要切换用户,要使用如下命令:
template1=# \!psql -U tk -d template1
9.设定用户特定的权限
还是要用例子来说明:
创建一个用户组:
sales=# CREATE GROUP sale;
添加几个用户进入该组
sales=# ALTER GROUP sale ADD USER sale1,sale2,sale3;
授予用户级sale针对表employee和products的SELECT权限
sales=# GRANT SELECT ON employee,products TO GROUP sale;
在sale中将用户user2删除
sales=# ALTER GROP sale DROP USER sale2;
10.备份数据库
可以使用pg_dump和pg_dumpall来完成。比如备份sales数据库:
pg_dump sales>/home/tk/pgsql/backup/1.bak

Datatable static image not found on the server

When you use ```datatables.min.css``` and ```datatables.min.js``` locally, instead of datatables CDN, you may have encountered that ```sort...