-
-
Save morice3/68c398765977e875b020fe5c21551207 to your computer and use it in GitHub Desktop.
Batch import all *.sql files in a folder recursively into your Postgres or PostgreSQL database
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# AUTHOR | |
# DANIEL E. GILLESPIE (2016) | |
# https://github.com/dantheman213 | |
# DESCRIPTION | |
# Import all *.sql files in a folder recuresively into your PostgreSQL database | |
# Batch export all table schemas and stored functions with this script located here: | |
# https://gist.github.com/dantheman213/aff70ee42a11f2d1fa46983878cd62e1 | |
# INSTALLATION | |
# 1. Install script at /usr/bin or /usr/sbin (if you | |
# want to make this root/admin privs only) | |
# 2. chmod +x /usr/bin/import_db_structure.sh | |
# 3. Make sure your Postgres database will accept a local | |
# connection with password authentication | |
# 4. Execute the script.. pass in import folder path as an argument | |
### CHANGE THESE TO YOUR SERVER/APP INFO ### | |
POSTGRES_USER="postgres" | |
POSTGRES_PASSWORD="postgres" | |
DATABASE_NAME="myapp_db" | |
### END CONFIGURATION ### | |
if [[ $# -eq 0 ]] ; then | |
echo 'Please enter the folder where you want to import all *.sql files under it.' | |
exit 0 | |
fi | |
echo "Cleaning up cache..." | |
/etc/init.d/postgresql stop | |
echo 1 > /proc/sys/vm/drop_caches | |
/etc/init.d/postgresql start | |
echo "Importing..." | |
export PGPASSWORD=$POSTGRES_PASSWORD | |
for file in `find $1 | grep -i '.sql'` | |
do | |
echo "importing $file" | |
psql -U $POSTGRES_USER $DATABASE_NAME < $file | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment