Postgresql

PostgreSQL is a powerful, open source object-relational database system. It has more than 15 years of active development and a proven architecture that has earned it a strong reputation for reliability, data integrity, and correctness.

It runs on all major operating systems, including Linux, UNIX (AIX, BSD, HP-UX, SGI IRIX, Mac OS X, Solaris, Tru64), and Windows. It is fully ACID compliant, has full support for foreign keys, joins, views, triggers, and stored procedures (in multiple languages).

It includes most SQL:2008 data types, including INTEGER, NUMERIC, BOOLEAN, CHAR, VARCHAR, DATE, INTERVAL, and TIMESTAMP. It also supports storage of binary large objects, including pictures, sounds, or video.

It has native programming interfaces for C/C++, Java, .Net, Perl, Python, Ruby, Tcl, ODBC, among others, and exceptional documentation.

http://www.postgresql.org/about/

Install Postgresql on CentOS 6.4

These installation steps should also apply to other distros based on RedHat source RPMs.

Logged in as root on the system run the following commands:

   1 yum install postgresql
   2 yum install postgresql-devel
   3 yum install postgresql-server
   4 service postgresql initdb
   5 chkconfig postgresql on
   6 service postgresql start

Edit the file /var/lib/pgsql/data/pg_hba.conf

   1 local all all ident
   2 host all all 0.0.0.0/0 password

To restart the postgresl DB run the command:

   1 service postgresql restart

Check postgresql version

   1 cat /var/lib/pgsql/data/PG_VERSION

Create postgresql test user and database

   1 su postgres
   2 psql

   1 create user appuser with login password '12345678';
   2 create database app;
   3 grant all privileges on database app to appuser;
   4 \q

Configure postgresql to accept all TCP connections

Edit the file /var/lib/pgsql/data/postgresql.conf

listen-address="*"
port = 5432
max_connections = 100

Restart again the postgresl DB:

   1 service postgresql restart

Test the connection using the user and password

   1 psql -U appuser -h 127.0.0.1 -W -d app

Backup and restore with pg_dump

   1 su postgres
   2 pg_dump databasex > /var/lib/pgsql/backDatabasex.sql
   3 psql databasex < /var/lib/pgsql/backDatabasex.sql # restore
   4 # pg_dumpall 
   5 pg_dumpall -f dump.sql 

Daily backup script

   1 #!/bin/sh
   2 #backupDBs.sh
   3 #chmod 755 backupDBs.sh 
   4 #backup database postgresql
   5 #crontab -e 
   6 #@daily /var/lib/pgsql/backupDBs.sh
   7 BACKUPFOLDER=/var/lib/pgsql/backups
   8 CURRDATE=`date -u "+%Y-%m-%dT%H:%m:%S.%Z"`
   9 FILE=$BACKUPFOLDER/backup$CURRDATE.sql
  10 DATABASE=dbx
  11 /usr/bin/pg_dump $DATABASE > $FILE
  12 gzip $FILE

Show table structure

   1 su postgres

   1 \c databasex
   2 \dt
   3 \d+ tablex

Drop a table column

   1 su postgres

   1 \c databasex
   2 alter table tablex drop column columnx;

Epoch from date field

Number of seconds since 1st January 1970 UTC.

   1 SELECT field1, field2, field3, extract(epoch from begindate) as begindate,extract(epoch from enddate) as enddate 
   2 FROM tableX 

Garbage-collect and analyze a PostgreSQL database

Create file /var/lib/pgsql/vacuumdb.sh and chmod 755 it

   1 #!/bin/sh
   2 /usr/bin/vacuumdb --all --analyze --verbose

Create cron job to user postgres, crontab -e

@daily /var/lib/pgsql/vacuumdb.sh

Install in debian buster

   1 apt install postgresql-11
   2 netstat -at -n | grep 5432
   3 ss -a -n | grep 5432
   4 
   5 # pg_hba.conf
   6 host all all 0.0.0.0/0 password
   7 # postgresql.conf
   8 listen-address="*"
   9 
  10 # change postgres pass
  11 sudo bash
  12 su - postgres 
  13 psql 
  14 alter user postgres with password '<define pass here>';

Give super user rights to user

   1 ALTER USER appuser WITH SUPERUSER;
   2 ALTER USER appuser WITH NOSUPERUSER;

List databases

   1 su postgres
   2 psql
   3 postgres=# \l
   4 

Run in docker container

   1 docker network create mynet
   2 docker run -p 5432:5432 --rm --name postgres-server -e POSTGRES_PASSWORD=postgres --network mynet -d postgres:15.3-alpine
   3 docker exec -it postgres-server sh
   4 psql -U postgres
   5 \l
   6 \q
   7 exit

Create schema

A schema organizes database objects into logical groups to make them more manageable.

   1 create schema if not exists my_schema;

Insert record with sequence value and current date

   1 insert into tablex (id, curdate)
   2 values( nextval('seqx'), current_date );

Search path

   1 -- Show current search path
   2 SHOW search_path;
   3 -- Set search path
   4 SET search_path TO myschema,public;

PostgreSQL (last edited 2024-03-14 22:42:06 by vitor)