Add script and README
This commit is contained in:
		
							parent
							
								
									179bb2fe98
								
							
						
					
					
						commit
						5a1be4f930
					
				
							
								
								
									
										29
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										29
									
								
								README.md
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,29 @@ | ||||
| # Using multiple databases with official PostgreSQL Docker image | ||||
| 
 | ||||
| The [official recommendation](https://hub.docker.com/_/postgres/) for creating | ||||
| multiple databases is as follows: | ||||
| 
 | ||||
| *If you would like to do additional initialization in an image derived from | ||||
| this one, add one or more `*.sql`, `*.sql.gz`, or `*.sh` scripts under | ||||
| `/docker-entrypoint-initdb.d` (creating the directory if necessary). After the | ||||
| entrypoint calls `initdb` to create the default `postgres` user and database, | ||||
| it will run any `*.sql` files and source any `*.sh` scripts found in that | ||||
| directory to do further initialization before starting the service.* | ||||
| 
 | ||||
| This directory contains a script to create multiple databases using that | ||||
| mechanism. | ||||
| 
 | ||||
| ## Usage | ||||
| 
 | ||||
| Clone the repository, mount it's directory into the official Docker image and | ||||
| declare database names separated by commas in `POSTGRES_MULTIPLE_DATABASES` | ||||
| environment variable as follows (`docker-compose` syntax): | ||||
| 
 | ||||
|     myapp-postgresql: | ||||
|         image: postgres:9.6.2 | ||||
|         volumes: | ||||
|             - ../docker-posgresql-multiple-databases:/docker-entrypoint-initdb.d | ||||
|         environment: | ||||
|             - POSTGRES_MULTIPLE_DATABASES=db1,db2 | ||||
|             - POSTGRES_USER=myapp | ||||
|             - POSTGRES_PASSWORD= | ||||
							
								
								
									
										22
									
								
								create-multiple-postgresql-databases.sh
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										22
									
								
								create-multiple-postgresql-databases.sh
									
									
									
									
									
										Executable file
									
								
							| @ -0,0 +1,22 @@ | ||||
| #!/bin/bash | ||||
| 
 | ||||
| set -e | ||||
| set -u | ||||
| 
 | ||||
| function create_user_and_database() { | ||||
| 	local database=$1 | ||||
| 	echo "  Creating user and database '$database'" | ||||
| 	psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL | ||||
| 	    CREATE USER $database; | ||||
| 	    CREATE DATABASE $database; | ||||
| 	    GRANT ALL PRIVILEGES ON DATABASE $database TO $database; | ||||
| 	EOSQL | ||||
| } | ||||
| 
 | ||||
| if [ $POSTGRES_MULTIPLE_DATABASES ]; then | ||||
| 	echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES" | ||||
| 	for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do | ||||
| 		create_user_and_database $db | ||||
| 	done | ||||
| 	echo "Multiple databases created" | ||||
| fi | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user