Add rows into Database table using Shell Script? - postgresql

How to add rows into table in the Database by Shell Script?
I have following:
Postgres Database Name: dm
Table Name: Error Codes
Password of Database: ******
I want to add rows into table?
I am just starting Shell Scripting...
My code:
#!/bin/sh
DATABASE="dm"
USERNAME="postgres"
HOSTNAME="HBG"
export PGPASSWORD="postgres"
psql -h $HOSTNAME -U $USERNAME $DATABaSE << EOF
select * from Error Codes
EOF
Getting error:
psql: could not connect to server: Connection refused
Is the server running on host "HBG" (192.168.0.241) and accepting
TCP/IP connections on port 5432?

Have a look on how to use the psql command.
You can state any SQL query using this command. For example:
psql -c "SELECT * FROM foo" mydatabase myusername
Of course you can also use INSERT, UPDATE and all other SQL commands, as long as myusername has the rights to do so.
If you want to do this WITHOUT a password (but this is strongly disrecommended and unsecure) you can add this at the start of your script:
set PGPASSWORD=<password>
See here for full documentation: http://www.postgresql.org/docs/7.4/static/app-psql.html

Related

How to supply password to 'psql' command while running PostgreSQL in Google Colab

I have installed PostgreSQL server in Google Colab as follows :
# Install postgresql server
!apt update > /dev/null
!apt install postgresql > /dev/null
!service postgresql start
and have then configured the 'postgres' userid and database as follows :
# Setup a password `pass` for username `postgres`
!sudo -u postgres psql -U postgres -c "ALTER USER postgres PASSWORD 'pass';"
#
# Setup a database with name `praxis` to be used
!sudo -u postgres psql -U postgres -c 'DROP DATABASE IF EXISTS praxisdb;'
!sudo -u postgres psql -U postgres -c 'CREATE DATABASE praxisdb;'
Subsequently, I have created a table, inserted data and run the select command
!psql -h localhost -p 5432 -Upostgres -W -dpraxisdb -c 'create table dept ... ;'
!psql -h localhost -p 5432 -Upostgres -W -dpraxisdb -c "INSERT INTO Dept ... ;"
!psql -h localhost -p 5432 -Upostgres -W -dpraxisdb -c "select * from dept;"
All this works perfectly but each time, I am prompted to enter the password. I wish to avoid having to enter the password each time. Based on what I read in the documentation on using password files, I created a file ~/.pgpass as follows :
!echo "localhost:5432:praxisdb:postgres:pass" > ~/.pgpass
!chmod 0600 ~/.pgpass
Now, when I execute any command, eg.
!psql -c "select * from dept;"
I get the error
psql: error: FATAL: role "root" does not exist
Where does this role root come from? I looked at the file ~/.pgpass and noted that its owner and group is root and I changed that to postgres using chown, chgrp, but that does not solve the problem. What else should I do in this case to solve the problem.
The version of Postgres and the OS is as follows :
!sudo -u postgres psql -V
psql (PostgreSQL) 12.13 (Ubuntu 12.13-0ubuntu0.20.04.1)
You misunderstand how the password file works. You still have to specify host, port, database and user in your connection request. The client library then searches the matching entry in the password file and reads the password.

Restoring the Dump using PostgreSQL's psql

I'm trying to restore the DB from an SQL file using
psql dbname < dumpfile
but after I connect to my DB psql provides a prompt with the name of the database to which psql is currently connected, followed by the string =>. For example:
testdb=>
So how can I enter the command, or which Meta-Command should I use to restore the DB with the psql command? (Already connected to my DB)
psql is the command line client. You cannot call psql when you are already in an interactive psql session.
Either you connect with psql -d testdb in an interactive session and import the dump with \i dumpfile, or you stay on the shell and call psql in a non-interactive session with the dump file as script: psql -d testdb -f dumpfile
you can use \c <database_name>
it will connect you to the desired database
for example
global=> \l
List of databases
Name | Owner | Encoding
test | pgadmins | UTF8
global=> \c test
You are now connected to database "test" as user "user1".
test=>

pg_dump from Linux command line

I am trying to work out a way to perform a PostgreSQL pg_dump of a single DB via RHEL v6.10 command line. Now the system I am working on is that you have to shell into Bash 4.1 first before you get to PSQL. Now I can access the actual PSQL db using the following syntax from the command line which works fine:
'''sudo -u postgres -H --psql -pxxxxx -d db_name'''
If I enter the following syntax from the RHEL command line:
'''sudo su postgres'''
I end up in the bash-4.1 shell. When executing the following command from within the shell: bash-4.1$ pg_dump db_name > /tmp/my_database.sql
I am presented with the following error:
pg_dump: [archiver (db)] connection to database "db_name" failed: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
I am expecting the db to be exported to /tmp/my_database.sql file. I have tried various combinations of commands from both RHEL and bash command lines but cannot achieve the desired outcome. Your assistance would be greatly appreciated, thank you.
Try this:
Connect as 'postgres' user
sudo -i -u postgres
Then run pg_dump, e.g.
pg_dump --blobs --file /your/backup/file/path/backupfilename.pgsql --dbname your_db_name
After that, you will find your backup file in /your/backup/file/path/backupfilename.pgsql
You can add many other options in pg_dump, See PostgreSQL Documentation

Postgres - npm script to drop/add database

I want to simplify my life and automate the process of adding/dropping my test db via an npm script, however I am running into issues.
Attempt 1:
"drop-db:local": "psql postgres \"drop database blog_db; create database blog_db; \\c blog_db; CREATE EXTENSION \"pgcrypto\";\""
After running this, I keep getting the following error
psql: error: could not connect to server: FATAL: Peer authentication failed for user "drop database blog_db; create database blog_db; \c "
Attempt 2:
changed
psql postgres
to
psql -h localhost -U rm postgres
So this opens the db in my terminal but that seems to ignore some stuff as mentioned in the msg below
psql: warning: extra command-line argument "drop database blog_db; create database blog_db; \c blog_db; CREATE EXTENSION pgcrypto;" ignored
What am I doing wrong?
This is a list of my db users
postgres=# \du
List of roles
Role name | Attributes | Member of
-----------+------------------------------------------------------------+-----------
postgres | Superuser, Create role, Create DB, Replication, Bypass RLS | {}
rm | Superuser, Create DB | {}
db version: psql (12.2 (Ubuntu 12.2-2.pgdg18.04+1))
You need to use -c or -f with your psql command.
As the psql help shows:
-c, --command=COMMAND run only single command (SQL or internal) and exit
-f, --file=FILENAME execute commands from file, then exit
As you are using multiple commands so it will be better of you use -f followed by a sql file name that has all the commands e.g your drop_create_db.sql file can have following code:
drop database blog_db;
create database blog_db;
\c blog_db;
CREATE EXTENSION "pgcrypto";
And you may run this file by using the following command
"drop-db:local": psql -U postgres -d postgres -p 5432 -f /tmp/drop_create_db.sql

executing query from docker to psql db shows psql: warning: extra command-line argument "" ignored

Hi I am in my docker console and trying to run shell script to connect to PSQL DB and execute queries.
#!/bin/sh
query = "select * from default$default."LightZone"";
#psql -U postgres -p postgres psql -e "$query";
This is my tablecreation.sh file and when I do ./tablecreation.sh
./tablecreation.sh: 5: ./tablecreation.sh: query: not found
psql: warning: extra command-line argument "" ignored
Password for user prisma:
psql (10.3 (Debian 10.3-1.pgdg90+1))
Type "help" for help.
The above message comes . But the same query is running from the psql command prompt.
can someone help me if i am missing something . I want to connect to postgres->prisma db >default$default schema > LightZone table and run some queries.
Escape the double quotes with \
query = "select * from default$default.\"LightZone\"";