Adding postgresql database import command to creation - sql

This is my shell command for creating a database. It is run as part of a deployment script to automatically create two databases without human intervention.
# POSTGRES
apt-get install -y postgresql
echo "CREATE ROLE deploy LOGIN ENCRYPTED PASSWORD '$APP_DB_PASS';" | sudo -u postgres psql
su postgres -c "createdb db1 --owner deploy"
su postgres -c "createdb db2 --owner deploy"
service postgresql reload
Within this code, could someone please explain how I can integrate importing a SQL file into postgresql within this stage.
I believe it is something like this but I haven't go that to work:
psql --username=postgres < /etc/schema.sql

On debian, you probably need to follow Daniel's comment because PostgreSQL is probably configured to require the OS user to be the same username as the db user.
So you need to
su postgres -c "psql -f /etc/schema.sql"
Alternatively you could put all these db creation commands in a nice little shell script (without the su postgres -c part) and then just run that all at once. Something like:
#!/bin/bash
echo "CREATE ROLE deploy LOGIN ENCRYPTED PASSWORD '$APP_DB_PASS';" | su postgres -c psql
createdb db1 --owner deploy
createdb db2 --owner deploy
psql db1 -f /etc/schema.sql
psql db2 -f /etc/schema.sql
Then you can run that with sudo or su -c and simplify the things that can go wrong, auth-wise.

Related

running postgresql image with podman failed

When running postgresql alpine image with podman :
podman run --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=test -e POSTGRES_USER=test -d postgres:11-alpine
the result is :
Error: /usr/bin/slirp4netns failed: "open(\"/dev/net/tun\"): No such device\nWARNING: Support for sandboxing is experimental\nchild failed(1)\nWARNING: Support for sandboxing is experimental\n"
The running system is archlinux. Is there a way to fix this error or a turn arround ?
Thanks
Is slirp4netns correctly installed? Check the project Site for information.
Sometimes the flag order matters. try -d first and -p last (directly infornt of the image) looking like:
podman run -d --name postgres -e POSTGRES_PASSWORD=test -e POSTGRES_USER=test -p 5432:5432 postgres:11-alpine
Try only creating the neccessary password, then log into your container and create manually (this always worked for me)
podman run -d --name postgres -e POSTGRES_PASSWORD=test -p 5432:5432 postgres:11-alpline
podman exec -it postgres bash
Create default user postgres
su - postgres
start postgres
psql
create databases and tables
CREATE USER testuser WITH PASSWORD 'testpassword' | Doku
CREATE DATABASE testdata WITH OWNER testuser
Check if it worked
\l+
Connect to your Database via IP and Port
I assume you upgraded Arch packages recently. Most likely your system needs a restart.

Netezza : Fetch all database names and write to a file in unix directory

I want to create a shell script which runs a Netezza SQL and create lookup file dbnames.lkp in a unix directory
lookup file name : dbnames.lkp
Could someone help me with nzsql command. I know just to connect to Netezza server using nzsql command and then run the queries. But my requirement is that the shell script should run connect and write the result of SQL query to a lookup file
nzsql -u $user -pw $password -d $db -host $hostname
You can query Netezza System view _v_database to fetch all databases in the connected server, provided the user should have access.
You can create a shell script with below command and this will take care of it.
user=<user_name>
password=<password>
query="select database from _v_database;"
file=dbnames.lkp
dbname=<dbname_to_connect>
hostname=<server>
nzsql -u $user -pw $password -d $dbname -host $hostname -o $file -A -t -c "$query"
-c : will run only a single query and exit nzsql.
You can get more information from below link:
https://www.ibm.com/support/knowledgecenter/en/SSULQD_7.1.0/com.ibm.nz.adm.doc/r_sysadm_nzsql_command.html

Error in creation of postgresql from bash - Install & Import

I am trying to automate the install of debian with postgreSQL but I'm running into issues with my script. The database import of schema.sql into the db1 doesn't seem to be working, and I'm not sure if I even created the database correctly.
This is the code I am using:
# POSTGRES
apt-get install -y postgresql
echo "CREATE ROLE deploy LOGIN ENCRYPTED PASSWORD '$APP_DB_PASS';" | sudo -u postgres psql
su postgres -c "createdb db1 --owner deploy"
su postgres -c "createdb db2 --owner deploy"
service postgresql reload
# IMPORT SQL
psql --username=postgres spider < /etc/schema.sql
When I try to see if the database is created I get the following errors and the SQL import didn't seem to work.
root#li624-168:/etc/app# psql -U root spider
psql: FATAL: role "root" does not exist
root#li624-168:/etc//app# psql -U deploy spider
psql: FATAL: Peer authentication failed for user "deploy"
Can anyone tell me please where I have gone wrong?
Firstly, make sure you check result codes when executing commands. You can abort your bash script by adding set -e at the top. If any single command fails it will stop immediately.
Secondly, take another look at the error message:
Peer authentication failed for user "deploy"
You're trying to login as "deploy" and it seems to recognize the user-name. However, your operating-system user is not called "deploy", so peer auth fails. It looks like you want to login using a password, so set up your pg_hba.conf file to allow that.
Postgres databases are owned by Linux users. So, you need to create an user in postgres tha have the same name of your Linux user. then, you have to use the new user to create your db. Example:
My linux account is razcor
sudo su postgres -c 'createuser -d -E -R -S razcor'
this creates a postgres user
sudo su razcor -c "createdb db1 --owner razcor"
this creates my db
result:
razcor#ubuntu:~$ psql -U razcor db1
psql (8.4.17)
Type "help" for help.
db1=>
In your case create a user named: root
#Richard Huxton: yes, I agree.

Automating install and creation of postgresql databases in shell

I am trying to create two databases called spider and geo under postgresql via an automated shell script. This is the code so far.
apt-get install -y postgresql
echo "CREATE ROLE deploy LOGIN ENCRYPTED PASSWORD '$APP_DB_PASS';" | sudo -u postgres psql
su postgres -c "createdb spider --owner deploy"
su postgres -c "createdb geo --owner deploy"
/etc/init.d/postgresql reload
Can anyone please have a look and see if I am going about this the right way. Moreover, when I try to see if it works by running the following command I get an error:
root:~# psql -l
psql: FATAL: role "root" does not exist
Where have I gone wrong, and is there any way to improve this script?
Judging by the apt-get, your deployment platform is Ubuntu-(ish).
apt-get install -y postgresql
echo "CREATE ROLE deploy LOGIN ENCRYPTED PASSWORD '$APP_DB_PASS';" | sudo -u postgres psql
su postgres -c "createdb spider --owner deploy"
su postgres -c "createdb geo --owner deploy"
service postgresql reload
Then you should be able to log in by specifying a user on the command line:
psql -U root spider
or
psql -U deploy spider
Generally speaking, you're on the right track.

PostgreSQL Deployment Script Stalls

I have a bash shell deployment script (linode stackscript) which runs when I deploy my debian 6.0 server. The script runs as root and the script is as follows:
apt-get update
apt-get install postgresql postgresql-contrib pgadmin3
passwd postgres
su - postgres
psql -c "ALTER USER postgres WITH PASSWORD 'changeme'" -d template1
su - postgres
createdb mytestdb
psql mytestdb
I have two problems:
Firstly, when I run each line manually through shell it works, but when I run it as the stackscript it runs the line passwd postgres but nothing after it.
Secondly, when I run the line passwd postgres it asks me to put in my password manually. Is there any way I can put it in as a variable in the shellscript?
passwd is meant to be used interactively.
The proper command to change a password in a batch is chpasswd.
Example:
#!/bin/sh
echo 'postgres:newpassword' | chpasswd
Also note that the way your script does su - postgres does not look like it's normally done in non-interactive mode.
Better do: su -c 'command1; command2...' - postgres