Postgres, PostgreSQL, psql, pgsql ... take your pick. Here are some handy commands if (like me) you cannot remember what you have to do to issue SQL commands against a Postgres table.
sudo systemctl start postgresql
sudo -u postgres psql -d databasename
\? | help |
\q | quit psql |
\l | list database names |
\c dbname | connect to database |
\dn * | display schema names |
\dt schema.* | display table names in schema |
\d tablename | display column names in table |
\du | display user names |
\cd dirname | change local directory |
\i sqlfile | include SQL commands from a file |
sudo -u postgres createuser username sudo -u postgres dropuser username
create database databasename; alter database databasename owner to username; grant connect on database databasename to username; drop database databasename;
create table tablename (columnname CHAR(8), columnname VARCHAR, columnname INT, columnname BYTEA); grant select on table tablename to username; insert into tablename (columnname, columnname, columnname, columnname) values ('text', 'text', nnn, '\xC1C2C340'); select * from tablename; update tablename set columnname = value where columnname = value; drop table tablename;
set search_path to schemaname, public; show search_path;or specify schemaname.tablename instead of tablename.
sudo -u postgres psql -c "show config_file;" sudo vi /var/lib/pgsql/data/postgresql.conf sudo systemctl restart postgresql
CREATE OR REPLACE LANGUAGE plpythonu; CREATE OR REPLACE FUNCTION ea(IN input_str BYTEA) RETURNS TEXT IMMUTABLE AS $$ from codecs import getdecoder codec = getdecoder('cp1140') result_str = codec(input_str, r'strict') return result_str[0] $$ LANGUAGE plpythonu; CREATE OR REPLACE FUNCTION ae(IN input_str TEXT, IN length INT) RETURNS BYTEA IMMUTABLE AS $$ from codecs import getencoder codec = getencoder('cp1140') if length is None: str = input_str else: str = input_str.ljust(length) result_str = codec(str, r'strict') return result_str[0] $$ LANGUAGE plpythonu;
sudo -u postgres psql -d databasename -f ebcdic.sql
create table dsntab (ebcdic_dsname BYTEA, ebcdic_volser BYTEA); insert into dsntab (ebcdic_dsname, ebcdic_volser) values (ae('SYS1.SVCLIB',44), ae('SYSRES',6)), (ae('SYS1.LINKLIB',44), ae('SYSRES',6)), (ae('USER.LINKLIB',44), ae('USER01',6));
select ea(ebcdic_dsname) as dsname, ea(ebcdic_volser) as volser from dsntab where ebcdic_volser = ae('SYSRES',6);
pg_dump -U postgres databasename > mypgdump.sql
scp mypgdump.sql username@ip-addr:
sudo -u postgres psql drop database if exists databasename; create database databasename; quit
sudo -u postgres psql -d databasename -f mypgdump.sql
HTML hand-crafted by Roger Bowler Last updated 21 Feb 2021