I m using Docker container it runs scripts in order 1-db.sql, 2-schemas.sql, 3-tables.sql located in db_init directory
1-db.sql looks like:
CREATE DATABASE my_db;
2-schemas.sql
CREATE SCHEMA my_db.credentials
AUTHORIZATION user1;
3-tables.sql
CREATE TABLE my_db.credentials."user_credentials"
(
id integer NOT NULL,
username character varying(40) NOT NULL,
password character varying(120) NOT NULL,
PRIMARY KEY (id)
);
Docker command i use (excluding password variables and port mappings) is:
docker run --name $CONTAINER_NAME -v $PWD/db_init/:/docker-entrypoint-initdb.d/ -d -t kartoza/postgis
I copy those files to correct directory and they are executed in correct order, however I get messages 'cross-database references are not implemented'
How to specify so that all of that corresponds to same database my_db (it was added to first database gis)?
Related
I am trying to create a master key for my database, needed because I follow a tutorial to make cross db queries.
Tutorial: https://www.scarydba.com/2016/03/21/cross-database-queries-in-azure-sql-database/
It is a similar question to Cannot create master key for master database in azure sql, but I get the message:
Msg 15578, Level 16, State 1, Line 1 There is already a master key in
the database. Please drop it before performing this statement.
...when I am trying to create a master key which is encrypted. What do I do wrong and is there another option to make cross db calls with Azure?
I execute the following:
CREATE MASTER KEY ENCRYPTION BY PASSWORD='SUPERSTRONGPASSWORD'
GO
Regards
According my experience, the master key has already created and exist when we create the Azure SQL database.
When we create master key :
-- Create a db master key if one does not already exist, using your own password.
CREATE MASTER KEY ENCRYPTION BY PASSWORD='<EnterStrongPasswordHere>';
Since the master key is already exist, we could not create a new one.
For most situations, Azure SQL database cross db queries don't need create the master key.
For example, I use the bellow query to do cross db query from Mydatabase to Mydatabase2:
--The "username" and "password" should be the username and password used to log in into the Customers database.
CREATE DATABASE SCOPED CREDENTIAL ElasticDBQueryCred
WITH IDENTITY = '<username>',
SECRET = '<password>';
--To create an external data source, execute the following command on the Orders database:
CREATE EXTERNAL DATA SOURCE MyElasticDBQueryDataSrc WITH
(TYPE = RDBMS,
LOCATION = '<server_name>.database.windows.net',
DATABASE_NAME = 'Customers',
CREDENTIAL = ElasticDBQueryCred,
) ;
--Create an external table on the Orders database, which matches the definition of the CustomerInformation table:
CREATE EXTERNAL TABLE [dbo].[test]
( [id] [int] ,
[age] [int]
)
WITH
( DATA_SOURCE = MyElasticDBQueryDataSrc)
--query the table
select * from test
Test table in Mydatabase2:
Cross db query in Mydatabase:
For details, please reference: Get started with cross-database queries.
Hope this helps.
Is there a way in which I can get the table definition in a script that I can execute?
i.e.
I have a table "cities":
CREATE TABLE public.cities
(
name character(80) COLLATE pg_catalog."default" NOT NULL,
location point,
CONSTRAINT pk_city_name PRIMARY KEY (name)
)
WITH (
OIDS = FALSE
)
TABLESPACE pg_default;
ALTER TABLE public.cities
OWNER to postgres;
is there a way I can generate that with a script rather than using the GUI?
If you want to write your commands into a script file, and the run it from command line, you should use psql -f <filename>.
See https://www.postgresql.org/docs/9.2/static/app-psql.html
Thanks for the links and places to look.
for those reading what I done:
open cmd
navigate to C:\Program Files\PostgreSQL\10\bin> (or create a short cut)
pg_dump -d mydb -t cities -U postgres -h localhost > C:/test/weather.sql
enter password for postgres
file is output to directory.
I know that in Oracle it's possible to create stored dblink and after that use it in query. For example:
Script for creation dblink:
CREATE PUBLIC DATABASE LINK my_link CONNECT TO my_schema IDENTIFIED BY shema_password USING 'remote';
And after that we can use it in our queries:
SELECT * FROM some_table#my_link;
I didn't find same solution for Postgres. I undestood that we can create named dblink connection:
For this we must use dblink_connect with name param. But created named dblink will destroy after session close.
Or we can create dblink connection for every queries:
SELECT *
FROM dblink('host= port= dbname= user= password=',
'select table_schema, table_name from information_schema.tables where table_schema = ''data''') AS t1 (table_schema TEXT, table_name TEXT);
Is it possible create stored dblink in Postgres and use it in different queries? Or I should create some function that return dblink connection params which encapsulate them?
I try use foreign table and do next steps:
Create postgres_fdw extension:
CREATE EXTENSION IF NOT EXISTS postgres_fdw;
Create Server:
CREATE SERVER my_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '-', port '-', dbname '-');
And create mapping for user 'sys' where set remote user and password:
CREATE USER MAPPING FOR sys SERVER light_house OPTIONS ( USER 'remote_user', PASSWORD 'remove_password');
GRANT USAGE ON FOREIGN SERVER my_server TO sys;
Create foreign table in schema:
CREATE FOREIGN TABLE system.my_local_table (
colums ..
) SERVER my_server OPTIONS (schema_name 'remote_user', table_name 'remote_table'
);
GRANT SELECT ON TABLE system.home_measurement TO argus_sys;
after that I catch next exception:
[2F003] ERROR: password is required
Description: Non-superuser cannot connect if the server does not request a password.
Help: Target server's authentication method must be changed.
You should use a foreign table.
To get rid of the error message, change the pg_hba.conf file on the remote database server to use md5 authentication (don't forget to reload with pg_ctl reload).
I wanted to run the alter table command using bash script. I managed to create the table, load the basemodel, create config tables and etc. The script will login to the postgres database before it is execute the alter table command. It stuck as (abcdb=> ) without proceed to the alter table command. Is there any way to make sure the alter table able to execute?
The login as
psql -h 191.169.51.10 -d abcdb -U myname
alter table attr_config rename regexp to regexp_val;
alter table class_action_config rename type to type_name;
alter table funcitem_config rename type to type_name;
In order to run a script like this you need to redirect the SQL/DML (alter table statements) into the psql command. Otherwise bash won't understand what to do with them.
psql -h 191.169.51.10 -d abcdb -U myname << EOF
alter table attr_config rename regexp to regexp_val;
alter table class_action_config rename type to type_name;
alter table funcitem_config rename type to type_name;
EOF
Alternatively you can put your SQL/DML into a separate file and have psql to read from that:
psql -h 191.169.51.10 -d abcdb -U myname < alter_statements.sql
Or
psql -h 191.169.51.10 -d abcdb -U myname -f alter_statements.sql
So what I want to do here is to run a script while connected to a database I already had using pgAdmin3. The script contains a create role, tablespace, database and a create schema and several tables under that schema.
The problem here is that when I run the script it creates the new role, tablespace and database correctly. It also creates the schema and the tables correctly but with a problem, the schema is created under the database, from which I ran the script, instead of the newly created database. The script is more or less like this.
CREATE ROLE "new_role" ... ;
CREATE TABLESPACE "new_space"
OWNER "new_role"
LOCATION '/home/...';
CREATE DATABASE "new_db"
WITH OWNER = "new_role"
TABLESPACE = "new_space";
CREATE SCHEMA "schema" AUTHORIZATION "new_role" ;
CREATE TABLE IF NOT EXISTS "schema"."new_table"(
...
) TABLESPACE "new_space";...
...
I already saw a solution with a \connect foo; but that is not what I wanted, I wanted it to somehow connect within the script without running things separately and running \connect foo in the terminal.
Can anyone tell me if there is anyway to do this and help me come out with a solution to this problem?
Use psql and split it up into two scripts . You can save the scripts in .sql files, and then run psql to connect to the DB you want to run each script against all on the same command line (with && in between each command). The two psql commands could be combined into one bash script so it's only one command that you need to run.
Something like this, if the script were named foo.sql:
psql -X -h <host> -U <user> -p <port> -f foo.sql <db_name>
The first script could have the create role, create tablespace and create database commands, connecting to the postgres db or a template DB, and the second script could have the rest of the commands.
You could also use createdb from the bash script instead of CREATE DATABASE.
Using pgAdminIV:
1- right click on default database "postgres"
2- select create database, give a name f.e. "newdatabase"
3- click on "newdatabase" (to establish connection)
4- open the query tool
5- import, write or paste your code
6- run your code f.e.: CREATE SCHEMA newschema;
It works for me...