Monday, November 19, 2018

▷ PostgreSQL import/export

[ postgresql export/import ]


1. Simple queries
# psql -c 'select * from test'
or
# echo "select * from test;" | psql -d mydb

2. Shell supports sql script.
#!/bin/sh

echo "before database connection"

psql -d mydb << EOF
create table foo (x integer);
insert into foo values (10);
select * from foo;
drop table foo;
EOF

echo "after database connection"

3. Feed sql statement to the input of psql command through '|'.
# echo sql_statement | psql -h remote_server -U username -d database
==>
sql_statements=""
for each line
  sql_statement="insert whatever you want;"
  sql_statements="$sql_statements $sql_statement"
done

echo $sql_statements | psql ...

4. Bulk insert than insert per row using insert statement.
# psql -d mydb
mydb=#
==>
insert into mytable (col1, col2, col3) values
 (1, 'foo', 'bar')
,(2, 'goo', 'gar')
,(3, 'hoo', 'har')
...
;

5. Bulk insert using a file with insert statement.
# psql -h remote_server -U username -d database -p 5432 -f my_insert_file.sql

6. Export to txt(csv) file.
# psql -d mydb
mydb=# \copy test to '/tmp/test.csv';
mydb=# \! cat /tmp/test.csv;
==>
7369    smith   clerk   7902    1980-12-17      800     \n      20
7499    allen   salesman        7698    1981-02-20      1600    300     30
7521    ward    salesman        7698    1981-02-22      1250    500     30
7566    jones   manager 7839    1981-04-02      2975    \n      20
7654    martin  salesman        7698    1981-09-28      1250    1400    30
7698    blake   manager 7839    1981-05-01      2850    \n      30
7782    clark   manager 7839    1981-06-09      2450    \n      10
7788    scott   analyst 7566    1982-12-09      3000    \n      20
7839    king    president       \n      1981-11-17      5000    \n      10
7844    turner  salesman        7698    1981-09-08      1500    0       30
7876    adams   clerk   7788    1983-01-12      1100    \n      20
7900    james   clerk   7698    1981-12-03      950     \n      30
7902    ford    analyst 7566    1981-12-03      3000    \n      20
7934    miller  clerk   7782    1982-01-23      1300    \n      10

mydb=# \copy test(a,b) to '/tmp/test2.csv';
mydb=# \! cat /tmp/test2.csv;
==>
7369    smith
7499    allen
7521    ward
7566    jones
7654    martin
7698    blake
7782    clark
7788    scott
7839    king
7844    turner
7876    adams
7900    james
7902    ford
7934    miller

mydb=# \copy test(a,b) to '/tmp/test3.csv' with delimiter ',' csv header;
mydb=# \! cat /tmp/test3.csv;
==>
empno,ename
7369,smith
7499,allen
7521,ward
7566,jones
7654,martin
7698,blake
7782,clark
7788,scott
7839,king
7844,turner
7876,adams
7900,james
7902,ford
7934,miller

7. Import from txt(csv) file.
# cat test.csv
empno,ename
7369,smith
7499,allen
7521,ward
7566,jones
7654,martin
7698,blake
7782,clark
7788,scott
7839,king
7844,turner
7876,adams
7900,james
7902,ford
7934,miller

mydb=# \copy test(a,b) from '/tmp/test.csv' with delimiter ',' csv header;
mydb=# select * from test;

No comments:

Post a Comment

◈ Recent Post

▷ UITest demo with TestOne (Mobile, Keypad and Drag until found tip)

[ UITest Demo Environment ] 1. UITest Solution: TestOne 2. Description 데모 설명    How to use keypad, and to drag until found.     키패드를...

◈ Popular Posts