Skip to main content

Posts about postgres

PostgreSQL on CentOS7

install

yum install postgresql-server
systemctl list-unit-files  -t service
systemctl is-enabled postgresql.service

start daemon

systemctl status postgresql.service
postgresql-setup initdb
systemctl start postgresql.service
su - postgres
psql -l

sql command operation

psql << END
\du
\q
END
psql -c "select * from pg_user"

create and drop user

\du
SELECT * FROM pg_user;
createuser -P ossdb
dropuser -i ossdb
CREATE USER ossdb LOGIN PASSWORD 'password';
DROP USER IF EXISTS ossdb;

create and drop database

\l
SELECT * FROM pg_database;
createdb -O ossdb -W ossdb
dropdb -i ossdb
CREATE DATABASE ossdb OWNER ossdb;
DROP DATABASE IF EXISTS ossdb;

connect

cat << END >> /var/lib/pgsql/data/pg_hba.conf
host    ossdb           ossdb           127.0.0.1/32            md5
END
systemctl reload postgresql.service
psql -U ossdb -d ossdb -h 127.0.0.1

create and drop table

\dit
\d table_name
CREATE TABLE t1 (
c1 integer,
c2 date,
c3 time,
c4 character,
c5 character varying(16),
c6 real,
primary key (c1), 
unique (c2, c3)
);
DROP TABLE IF EXISTS  t1;