Using r sf::st_write to non-public schema in PostgreSQL












1















I'm trying to write a spatial table to a schema that is not the default public schema in a PostgreSQL db.



library(sf)
library(DBI)
library(RPostgreSQL)
library(spData)

# PostgreSQL DB parameters
host <- "myHost"
port <- 5432
username <- "myName"
dbname <- "myDb"
password <- "MyPassword"

# Connect to db
conn <- dbConnect(PostgreSQL(), dbname = dbname, host = host, port = port, user = username, password = password)

st_write(obj = cycle_hire, dsn = conn, Id(schema="myOtherSchema", table = "myCycle")) # Write data to db - currently only writes to default schema

# Disconnect db
dbDisconnect(conn)


But this adds my table to the public schema with the name "myOtherSchema"."myCycle".



Also tried above with...



dbWriteTable(conn = conn, name = "myCycle", value = cycle_hire, Id(schema="mySchema"))



...substituted for st_write, which results in myCycle being written to public schema.



What am I doing wrong?



Session info:



R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server >= 2012 x64 (build 9200)


Running PostgreSQL 11.1 on Centos 7 OS.










share|improve this question

























  • I'd try connecting using package RPostgres: conn <- dbConnect(RPostgres::Postgres()), dbname = dbname, host = host, port = port, user = username, password = password)`

    – lbusett
    Jan 15 at 21:28











  • @lbusett there's no package RPostgres

    – mark
    Jan 15 at 21:51











  • Did you try installing it (install.packages("RPostgres") ?

    – lbusett
    Jan 15 at 21:57











  • @lbusett ah ha. See what just google searching brings you. OK ran conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, user = username, password = password) then st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle")) and get Error in st_write.sf(obj = cycle_hire, dsn = conn, Id(schema = "roads_spatial", : no st_write method available for dsn of class list

    – mark
    Jan 18 at 14:32











  • Scratch that, my bad: @lbusett Ran conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, user = username, password = password) then st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle")) and success! Many thanks - I wasn't aware of RPostgres. Post a solution and I'll accept.

    – mark
    Jan 18 at 14:55
















1















I'm trying to write a spatial table to a schema that is not the default public schema in a PostgreSQL db.



library(sf)
library(DBI)
library(RPostgreSQL)
library(spData)

# PostgreSQL DB parameters
host <- "myHost"
port <- 5432
username <- "myName"
dbname <- "myDb"
password <- "MyPassword"

# Connect to db
conn <- dbConnect(PostgreSQL(), dbname = dbname, host = host, port = port, user = username, password = password)

st_write(obj = cycle_hire, dsn = conn, Id(schema="myOtherSchema", table = "myCycle")) # Write data to db - currently only writes to default schema

# Disconnect db
dbDisconnect(conn)


But this adds my table to the public schema with the name "myOtherSchema"."myCycle".



Also tried above with...



dbWriteTable(conn = conn, name = "myCycle", value = cycle_hire, Id(schema="mySchema"))



...substituted for st_write, which results in myCycle being written to public schema.



What am I doing wrong?



Session info:



R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server >= 2012 x64 (build 9200)


Running PostgreSQL 11.1 on Centos 7 OS.










share|improve this question

























  • I'd try connecting using package RPostgres: conn <- dbConnect(RPostgres::Postgres()), dbname = dbname, host = host, port = port, user = username, password = password)`

    – lbusett
    Jan 15 at 21:28











  • @lbusett there's no package RPostgres

    – mark
    Jan 15 at 21:51











  • Did you try installing it (install.packages("RPostgres") ?

    – lbusett
    Jan 15 at 21:57











  • @lbusett ah ha. See what just google searching brings you. OK ran conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, user = username, password = password) then st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle")) and get Error in st_write.sf(obj = cycle_hire, dsn = conn, Id(schema = "roads_spatial", : no st_write method available for dsn of class list

    – mark
    Jan 18 at 14:32











  • Scratch that, my bad: @lbusett Ran conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, user = username, password = password) then st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle")) and success! Many thanks - I wasn't aware of RPostgres. Post a solution and I'll accept.

    – mark
    Jan 18 at 14:55














1












1








1


1






I'm trying to write a spatial table to a schema that is not the default public schema in a PostgreSQL db.



library(sf)
library(DBI)
library(RPostgreSQL)
library(spData)

# PostgreSQL DB parameters
host <- "myHost"
port <- 5432
username <- "myName"
dbname <- "myDb"
password <- "MyPassword"

# Connect to db
conn <- dbConnect(PostgreSQL(), dbname = dbname, host = host, port = port, user = username, password = password)

st_write(obj = cycle_hire, dsn = conn, Id(schema="myOtherSchema", table = "myCycle")) # Write data to db - currently only writes to default schema

# Disconnect db
dbDisconnect(conn)


But this adds my table to the public schema with the name "myOtherSchema"."myCycle".



Also tried above with...



dbWriteTable(conn = conn, name = "myCycle", value = cycle_hire, Id(schema="mySchema"))



...substituted for st_write, which results in myCycle being written to public schema.



What am I doing wrong?



Session info:



R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server >= 2012 x64 (build 9200)


Running PostgreSQL 11.1 on Centos 7 OS.










share|improve this question
















I'm trying to write a spatial table to a schema that is not the default public schema in a PostgreSQL db.



library(sf)
library(DBI)
library(RPostgreSQL)
library(spData)

# PostgreSQL DB parameters
host <- "myHost"
port <- 5432
username <- "myName"
dbname <- "myDb"
password <- "MyPassword"

# Connect to db
conn <- dbConnect(PostgreSQL(), dbname = dbname, host = host, port = port, user = username, password = password)

st_write(obj = cycle_hire, dsn = conn, Id(schema="myOtherSchema", table = "myCycle")) # Write data to db - currently only writes to default schema

# Disconnect db
dbDisconnect(conn)


But this adds my table to the public schema with the name "myOtherSchema"."myCycle".



Also tried above with...



dbWriteTable(conn = conn, name = "myCycle", value = cycle_hire, Id(schema="mySchema"))



...substituted for st_write, which results in myCycle being written to public schema.



What am I doing wrong?



Session info:



R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server >= 2012 x64 (build 9200)


Running PostgreSQL 11.1 on Centos 7 OS.







r postgresql sf






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 15 at 16:04







mark

















asked Jan 15 at 15:50









markmark

126212




126212













  • I'd try connecting using package RPostgres: conn <- dbConnect(RPostgres::Postgres()), dbname = dbname, host = host, port = port, user = username, password = password)`

    – lbusett
    Jan 15 at 21:28











  • @lbusett there's no package RPostgres

    – mark
    Jan 15 at 21:51











  • Did you try installing it (install.packages("RPostgres") ?

    – lbusett
    Jan 15 at 21:57











  • @lbusett ah ha. See what just google searching brings you. OK ran conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, user = username, password = password) then st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle")) and get Error in st_write.sf(obj = cycle_hire, dsn = conn, Id(schema = "roads_spatial", : no st_write method available for dsn of class list

    – mark
    Jan 18 at 14:32











  • Scratch that, my bad: @lbusett Ran conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, user = username, password = password) then st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle")) and success! Many thanks - I wasn't aware of RPostgres. Post a solution and I'll accept.

    – mark
    Jan 18 at 14:55



















  • I'd try connecting using package RPostgres: conn <- dbConnect(RPostgres::Postgres()), dbname = dbname, host = host, port = port, user = username, password = password)`

    – lbusett
    Jan 15 at 21:28











  • @lbusett there's no package RPostgres

    – mark
    Jan 15 at 21:51











  • Did you try installing it (install.packages("RPostgres") ?

    – lbusett
    Jan 15 at 21:57











  • @lbusett ah ha. See what just google searching brings you. OK ran conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, user = username, password = password) then st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle")) and get Error in st_write.sf(obj = cycle_hire, dsn = conn, Id(schema = "roads_spatial", : no st_write method available for dsn of class list

    – mark
    Jan 18 at 14:32











  • Scratch that, my bad: @lbusett Ran conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, user = username, password = password) then st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle")) and success! Many thanks - I wasn't aware of RPostgres. Post a solution and I'll accept.

    – mark
    Jan 18 at 14:55

















I'd try connecting using package RPostgres: conn <- dbConnect(RPostgres::Postgres()), dbname = dbname, host = host, port = port, user = username, password = password)`

– lbusett
Jan 15 at 21:28





I'd try connecting using package RPostgres: conn <- dbConnect(RPostgres::Postgres()), dbname = dbname, host = host, port = port, user = username, password = password)`

– lbusett
Jan 15 at 21:28













@lbusett there's no package RPostgres

– mark
Jan 15 at 21:51





@lbusett there's no package RPostgres

– mark
Jan 15 at 21:51













Did you try installing it (install.packages("RPostgres") ?

– lbusett
Jan 15 at 21:57





Did you try installing it (install.packages("RPostgres") ?

– lbusett
Jan 15 at 21:57













@lbusett ah ha. See what just google searching brings you. OK ran conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, user = username, password = password) then st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle")) and get Error in st_write.sf(obj = cycle_hire, dsn = conn, Id(schema = "roads_spatial", : no st_write method available for dsn of class list

– mark
Jan 18 at 14:32





@lbusett ah ha. See what just google searching brings you. OK ran conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, user = username, password = password) then st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle")) and get Error in st_write.sf(obj = cycle_hire, dsn = conn, Id(schema = "roads_spatial", : no st_write method available for dsn of class list

– mark
Jan 18 at 14:32













Scratch that, my bad: @lbusett Ran conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, user = username, password = password) then st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle")) and success! Many thanks - I wasn't aware of RPostgres. Post a solution and I'll accept.

– mark
Jan 18 at 14:55





Scratch that, my bad: @lbusett Ran conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port, user = username, password = password) then st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle")) and success! Many thanks - I wasn't aware of RPostgres. Post a solution and I'll accept.

– mark
Jan 18 at 14:55












1 Answer
1






active

oldest

votes


















1














This happens because you are connecting to the db via package RPostgreSQL, but the syntax used for specifying table and schema is that used with connections made with package RPostgres. You can solve this using:



    require(RPostgres)
conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port,
user = username, password = password)
st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle"))





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54202315%2fusing-r-sfst-write-to-non-public-schema-in-postgresql%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    This happens because you are connecting to the db via package RPostgreSQL, but the syntax used for specifying table and schema is that used with connections made with package RPostgres. You can solve this using:



        require(RPostgres)
    conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port,
    user = username, password = password)
    st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle"))





    share|improve this answer




























      1














      This happens because you are connecting to the db via package RPostgreSQL, but the syntax used for specifying table and schema is that used with connections made with package RPostgres. You can solve this using:



          require(RPostgres)
      conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port,
      user = username, password = password)
      st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle"))





      share|improve this answer


























        1












        1








        1







        This happens because you are connecting to the db via package RPostgreSQL, but the syntax used for specifying table and schema is that used with connections made with package RPostgres. You can solve this using:



            require(RPostgres)
        conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port,
        user = username, password = password)
        st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle"))





        share|improve this answer













        This happens because you are connecting to the db via package RPostgreSQL, but the syntax used for specifying table and schema is that used with connections made with package RPostgres. You can solve this using:



            require(RPostgres)
        conn <- dbConnect(Postgres(), dbname = dbname, host = host, port = port,
        user = username, password = password)
        st_write(obj = cycle_hire, dsn = conn, Id(schema="roads_spatial", table = "myCycle"))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 18 at 17:27









        lbusettlbusett

        3,44921332




        3,44921332






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54202315%2fusing-r-sfst-write-to-non-public-schema-in-postgresql%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Liquibase includeAll doesn't find base path

            How to use setInterval in EJS file?

            Petrus Granier-Deferre